command 命令
#! /bin/bash
if command -v git >/dev/null 2>&1; then
echo 'exists git'
else
echo 'no exists git'
fi
type命令
#! /bin/bash
if type git >/dev/null 2>&1; then
echo 'exists git'
else
echo 'no exists git'
fi
hash命令
#! /bin/bash
if hash git 2>/dev/null; then
echo 'exists git'
else
echo 'no exists git'
fi
重定向
一般情況下,每個 Unix/Linux 命令運行時都會打開三個文件:
- 標(biāo)準(zhǔn) 輸入文件(stdin):stdin的文件描述符為0,Unix程序默認(rèn)從stdin讀取數(shù)據(jù)。
- 標(biāo)準(zhǔn)輸出文件(stdout):stdout 的文件描述符為1,Unix程序默認(rèn)向stdout輸出數(shù)據(jù)。
- 標(biāo)準(zhǔn)錯誤文件(stderr):stderr的文件描述符為2,Unix程序會向stderr流中寫入錯誤信息。
默認(rèn)情況下
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 文件
如果希望執(zhí)行某個命令,但又不希望在屏幕上顯示輸出結(jié)果,那么可以將輸出重定向到 /dev/null
command > /dev/null 2>&1
屏蔽 stdout 和 stderr
參考: