使用可选参数编写鱼壳脚本
我有一个鱼壳脚本,其默认行为是在完成时发送电子邮件。我想修改它以响应命令行中的nomail
参数。因此,例如,正常运行的脚本会产生一个电子邮件:使用可选参数编写鱼壳脚本
michaelmichael: ~/bin/myscript
但是,如果与nomail
开关运行,也不会发送确认邮件:
michaelmichael: ~/bin/myscript nomail
如果我用nomail
参数运行脚本,它运行良好。没有nomail
,是未定义的,它会引发错误。我搜查了鱼壳文档,但似乎找不到任何可行的工具。这里是我到目前为止
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
显然,期待一个说法,我只是不知道该语法告诉它不接受任何参数,或者如果存在的话。
我猜这是非常基本的,但我只是不明白shell脚本。
包装你switch
声明是这样的:
if set -q argv
...
end
另外,我觉得你默认情况下应该是case '*'
。
这工作(虽然'nomail'必须是默认情况下,而不是'*')。以下是我将它改为:'if set -q argv;开关$ argv;案件nomail;回声不发送邮件。案件 '*';回声未指定的开关。结束;其他;回声发送邮件。结束' – michaelmichael 2010-06-25 17:30:49
@michaelmichael:默认情况下,我的意思是'switch'语句的缺省值,如果没有指定的情况匹配,则执行该语句。您的评论中的代码完全如何。对不起,我误解了您的原始代码并误认为“???”就好像它打算成为'switch'默认值一样。 – 2010-06-25 18:48:20
如果您更喜欢使用switch语句它也可以:
switch (echo $argv)
case nomail
## Perform normal script functions
case '*'
## Perform normal script functions
mailx -s "Script Done!"
end
如果您发布的特定错误消息这是更有帮助。添加了 – 2010-06-25 14:29:37
。对于那个很抱歉。 – michaelmichael 2010-06-25 14:34:25