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.
To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.
Prompt Tab The "prompt" tab displays the contents of the current fish shell prompt. It allows selection from 17 predefined prompts. To change the prompt, select one and press "Prompt Set!".
To be able to run fish scripts from your terminal, you have to do two things. Add the following shebang line to the top of your script file: #!/usr/bin/env fish . Mark the file as executable using the following command: chmod +x <YOUR_FISH_SCRIPT_FILENAME> .
You can make FSH your default shell on the terminal via two simple steps: Add the line /usr/local/bin/fish to the /etc/shells file (it may already be there depending on how you have installed the shell). This is assuming FSH was installed in /usr/local/bin, as is the default location for when it is compiled.
Wrap your switch
statement like this:
if set -q argv
...
end
Also, I think your default case should be case '*'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With