2
A
回答
3
1
修订
尝试
[ -x "$executable" ] && echo "Command '$executable' not found" >&2 && exit 1
这将写入错误到stderr和退出1个退出代码。
如果您只有实用程序的名称您可以使用type
内建程序检查其路径。
实施例:
type type
type ls
type xls
输出:
type is a shell builtin
ls is /usr/bin/ls
./test.sh: line 13: type: xls: not found
测试返回1,如果没有找到效用。
所以如果$executable
可以是任何东西(一个bash内置的,别名,二进制,...),那么这可以用于:
type -p ls>/dev/null && ls -l
type -p xls>/dev/null && xls --some_arg
这将运行ls
(任何可执行文件),但不是xls。
无论如何,如果在脚本中没有设置execfail
选项(shopt
),那么脚本将在声明bash: some_utility: command not found
错误消息后退出。如果设置了该选项,则继续。但你可以trap
伪信号ERR
做什么,你需要:
shopt -s execfail
fnc() { echo $?, $_, Oops;}
trap fnc ERR
ls -d *|head -2
xls
yls
输出:
a1
a2
./test_tLcn.sh: line 8: xls: command not found
127, xls, Oops
./test_tLcn.sh: line 9: yls: command not found
127, yls, Oops
4
如果没有找到命令,退出状态应该是127.但是,你可能会使用bash
4或更高版本,并具有定义的称为command_not_found_handle
的功能。如果找不到命令,则调用此函数,并且它可能会退出0,屏蔽127代码。
运行type command_not_found_handle
将显示定义该函数的定义。您可以通过运行unset command_not_found_handle
来禁用它。
相关问题
- 1. 错误:=:找不到命令(bash脚本)
- 2. Bash脚本说找不到命令?
- 3. Bash shell脚本:f:找不到命令
- 4. Bash脚本“命令未找到错误”
- 5. bash脚本:命令没有找到
- 6. 在Docker bash脚本中运行时找不到命令
- 7. AIX 6.1如果在Bash脚本中找不到命令
- 8. 在bash脚本中找不到命令错误
- 9. cd命令不bash脚本
- 10. mysqldump bash脚本停止在密码“找不到命令”
- 11. bash脚本命令没有找到 - 命令正确实施
- 12. Bash脚本将不起作用:找不到命令
- 13. 命令在命令行中找到但在脚本中找不到
- 14. 在Git Bash中找不到javac命令
- 15. bash找不到HTK命令
- 16. BASH - 找不到xargs命令
- 17. 命令在$()中在bash脚本中
- 18. 不能grep命令resultats在bash脚本
- 19. java运行bash脚本,找不到命令
- 20. grep和cut bash脚本:找不到命令
- 21. 命令存在,但在bash脚本没有找到
- 22. 在bash脚本中传递scala命令
- 23. date命令在bash shell脚本中
- 24. 在bash脚本中执行sed命令
- 25. 在bash脚本中使用find命令
- 26. 在bash脚本中的读命令
- 27. 在bash脚本中执行Postgresql命令
- 28. 在bash脚本中运行hadoop命令
- 29. bash脚本+ grep + perl命令
- 30. PostgreSQL命令fom bash脚本
的[检查程序从一个bash脚本存在(HTTP可能重复/stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script) – blong