我汇总了以下内容来检测 Bash 是否正在运行脚本:
################################################################################
# Checks whether execution is going through Bash, aborting if it isn't. TinoSino
current_shell="$(
ps `# Report a snapshot of the current processes` \
-p $$ `# select by PID` \
-o comm `# output column: Executable namename` \
|\
paste `# Merge lines of files ` \
-s `# paste one file at a time instead of in parallel` \
- `# into standard output` \
|\
awk `# Pick from list of tokens` \
'{ print $NF }' `# print only last field of the command output`
)"
current_shell="${current_shell#-}" # Remove starting '-' character if present
if [ ! "${current_shell}" = 'bash' ]; then
echo "This script is meant to be executed by the Bash shell but it isn't."
echo 'Continuing from another shell may lead to unpredictable results.'
echo 'Execution will be aborted... now.'
return 0
fi
unset current_shell
################################################################################
我不是专门要求你对它进行代码审查,因为你会把我送到 CodeReview;我的问题是:
- 你将如何测试这个放在我脚本顶部的“执行警卫”是否确实可靠地完成了它的工作?
我正在考虑安装虚拟机并在每台机器上安装诸如zsh
,csh
等之类的东西。但这对我来说看起来太耗时了。更好的方法来做到这一点?
如果你发现一个直接的错误,请向我指出。我认为,只是挥舞着腿等待被压扁的刺眼虫子应该被压扁。