Reputation: 14125
I have a fish shell script whose default behavior is to send an email when complete. I'd like to modify it to respond to a nomail
argument from the command line. So, for example, running the script normally would produce an email:
michaelmichael: ~/bin/myscript
But if run with the nomail
switch, it wouldn't send the confirmation email:
michaelmichael: ~/bin/myscript nomail
If I run the script with the nomail
argument, it runs fine. Without nomail
, $argv
is undefined and it throws an error. I've scoured the fish shell documentation, but can't seem to find anything that will work. Here's what I have so far
switch $argv
case nomail
## Perform normal script functions
case ???
## Perform normal script functions
mailx -s "Script Done!"
end
Running this throws the following error:
switch: Expected exactly one argument, got 0
Obviously it expects an argument, I just don't know the syntax for telling it to accept no arguments, or one if it exists.
I'm guessing this is pretty basic, but I just don't understand shell scripting very well.
Upvotes: 12
Views: 7562
Reputation: 14149
If you prefer using switch statement it's also possible:
switch (echo $argv)
case nomail
## Perform normal script functions
case '*'
## Perform normal script functions
mailx -s "Script Done!"
end
Upvotes: 7
Reputation: 360015
Wrap your switch
statement like this:
if set -q argv
...
end
Also, I think your default case should be case '*'
.
Upvotes: 10