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
重定向
一般情況下,每個(gè) Unix/Linux 命令運(yùn)行時(shí)都會(huì)打開三個(gè)文件:
- 標(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)錯(cuò)誤文件(stderr):stderr的文件描述符為2舍沙,Unix程序會(huì)向stderr流中寫入錯(cuò)誤信息朵栖。
默認(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í)行某個(gè)命令陨溅,但又不希望在屏幕上顯示輸出結(jié)果终惑,那么可以將輸出重定向到 /dev/null
command > /dev/null 2>&1
屏蔽 stdout 和 stderr
參考: