题
我有一个鱼壳脚本,其默认行为是完成后发送电子邮件。我想修改它以回应 nomail
来自命令行的参数。因此,例如,运行脚本通常会产生电子邮件:
michaelmichael: ~/bin/myscript
但是如果与 nomail
切换,它不会发送确认电子邮件:
michaelmichael: ~/bin/myscript nomail
如果我用 nomail
论点,它运行良好。没有 nomail
, $argv
是未定义的,它会引发错误。我已经搜寻了鱼壳的文档,但似乎找不到任何有效的东西。这是我到目前为止的
switch $argv
case nomail
## Perform normal script functions
case ???
## Perform normal script functions
mailx -s "Script Done!"
end
运行这将引发以下错误:
switch: Expected exactly one argument, got 0
显然,它期望有一个论点,我只是不知道该语法告诉它不接受任何论点,也不知道它存在的语法。
我猜这是非常基本的,但是我只是不太了解壳牌脚本很好。
解决方案
包裹你的 switch
这样的陈述:
if set -q argv
...
end
另外,我认为您的默认情况应该是 case '*'
.
其他提示
如果您喜欢使用Switch语句,也可以:
switch (echo $argv)
case nomail
## Perform normal script functions
case '*'
## Perform normal script functions
mailx -s "Script Done!"
end
不隶属于 StackOverflow