Shell Script – How to Conditionally Pass an Argument from a POSIX Shell Script

argumentsposixshell-script

#!/bin/sh
echo "Noise $1"
echo "Enhancement $2"
for snr in 0 5 10 15 20 25
do
  python evaluate.py --noise $1 --snr 25 --iterations 1250 --enhancement $2
done

If $2 is not specified, I don't want to pass the --enhancement $2 argument to my python script. How would I do that?

Best Answer

Modifying your original script:

#!/bin/sh
echo "Noise $1"
echo "Enhancement $2"
for snr in 0 5 10 15 20 25
do
  python evaluate.py --noise "$1" --snr "$snr" --iterations 1250 ${2:+--enhancement "$2"}
done

The standard parameter expansion ${var:+word} will expand to word if the variable var is set and not empty. In the code above, we use it to add --enhancement "$2" to the command if $2 is available and not empty.

I've also taken the liberty to assume that what you are giving to --snr as an option-argument should be the loop variable's value.


My personal touch on the code (mostly just using printf rather than echo, avoiding long lines, and giving the code a bit more air):

#!/bin/sh

printf 'Noise %s\n' "$1"
printf 'Enhancement %s\n' "$2"

for snr in 0 5 10 15 20 25; do
    python evaluate.py \
        --noise "$1" \
        --snr "$snr" \
        --iterations 1250 \
        ${2:+--enhancement "$2"}
done

As mosvy points out in comments below: If your /bin/sh happens to be the dash shell, or some other shell that does not properly reset IFS as a new shell session starts (this is required by POSIX), and if you have, for one reason or other, exported IFS and given it a non-default value, then you may want to use unset IFS at the top of the above script.

Do that whenever you have fixed all other issues that exporting IFS doubtlessly have raised (don't export IFS).

Related Question