我目前正在开发一个脚本,需要检查程序是否在首次启动时安装。如果程序不存在,脚本将继续执行安装程序所需的任何操作。到目前为止,我已尝试过以下内容:
program_exist=$( type "someprogram" | grep "not found" )
if ! "$someprogram_exist"; then
do some stuff
fi
program_exist=$( type "someprogram" 2>&1 >/dev/null | grep "not found" )
if ! "$someprogram_exist"; then
do some stuff
fi
但每次运行时,我都会收到以下消息:
./some_program.sh: line 8: ./some_program.sh: line 7: type: someprogram: not found: No such file or directory
有没有办法检查程序是否存在,而不是每次都显示./some_program.sh: line 8: ./some_program.sh: line 7: type: someprogram: not found: No such file or directory
消息?
答案 0 :(得分:3)
试试这个:
if ! type someprogram &>/dev/null; then
do_some_stuff
fi
或更短:
type someprogram &>/dev/null || do_some_stuff