编辑
2021-10-19
DevOps
00
请注意,本文编写于 928 天前,最后修改于 654 天前,其中某些信息可能已经过时。

目录

重定向
判断命令是否存在
command
type
hash 命令
参考

重定向

一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:

  • 标准 输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

默认情况下:

command > file 将 stdout 重定向到 file command 2 > file 将 stderr 重定向到 file command < file 将stdin 重定向到 file。 command > file 2>&1 将 stderr 和 stdout合并后重定向到 file command > file1 < file2 将 stdout 重定向到 file1,stdin 重定向file2 command 2 >> file 将 stderr 追加到 file末尾

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null

command > /dev/null 2>&1 屏蔽 stdout 和 stderr

判断命令是否存在

command

bash
#! /bin/bash if command -v git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi

type

bash
#! /bin/bash if type git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi

hash 命令

bash
#! /bin/bash if hash git 2>/dev/null; then echo 'exists git' else echo 'no exists git' fi

参考

https://www.jianshu.com/p/fbffa5cc49e1 http://c.biancheng.net/cpp/view/2738.html

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:mereith

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!