Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我们的 shell 脚本的第一行是
#!/bin/bash
根据我的(有限的)Linux shell 知识/bin/bash,当用户直接调用此脚本时,这会导致使用启动应用程序,例如,使用
/bin/bash
$ ./myscript.sh
不幸的是,当用户使用
$ sh ./myscript.sh
如何捕捉“错误”外壳执行此脚本并向用户显示适当的错误消息的情况?
case "$BASH" in *bash) :;; *) echo >&2 "$0: please run this with bash."; exit 1;; esac
或者你可以为他们做:
case "$BASH" in *bash) :;; *) exec bash "$0" "$@";; esac