Pergunta

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.

Foi útil?

Solução

Wrap your switch statement like this:

if set -q argv
    ...
end

Also, I think your default case should be case '*'.

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top