unix - My exit commands are not working right
问题描述
I am not exactly sure if i am using the exits correctly. But when i execute the code with something that prints the usage statement it should stop there. It should do one or the other. In my case it is doing both.
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
fi
exit=1
path=$(echo $PATH | tr ":" " ")
for dir in $path
do
if [[ -x "$dir/$cmd" && -r "$dir/$cmd" ]]
then
echo "$dir/$cmd"
exit 0
fi
done
echo "$cmd not on $PATH"
exit=0
OUTPUT:
[112] ./findcmd
usage: ./findcmd command
/usr/local/bin/ **this should not be here
[113] ./findcmd ping
/usr/bin/ping
解决方案
应该是exit [n]
。
在脚本中,可以使用 exit nnn 命令将 nnn 退出状态传递给 shell(nnn 必须是 0 - 255 范围内的整数)。
它应该在 IF/FOR 块内。
像这样:
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
exit 1
fi
## rest of code to execute if args are correct
推荐阅读
- sql - 如何在熊猫中加入多个条件?
- javascript - 如何在 GlideRecord 上添加字段类型条件?
- c# - 是否有支持“组成”Unicode 组合字符的字体?
- javascript - GAS 如何处理 client_side 变量?
- c++ - 语法 'T<...>(...)' 是不可能的。下一个最好的事情是什么?
- python - 在熊猫数据框中按另一列计算值组之前的行数
- delphi - Delphi:挂钩 ToggleSwitch 手动状态更改以避免 Click 调用
- pdf - 使用我当前的脚本创建 PDF 横向
- python-3.x - 为什么在while循环的条件下会出现“属性错误”?但不是while循环中的if语句?
- python - DataFrame 和 Series 之间的元素乘法返回 NaN