0

I have the following bash script:

        while getopts g:s:l:o:t:f:rbh option
    do
        case "${option}" 
            in
            g) genome=${OPTARG};;
            s) species=${OPTARG};;
            l) RepSub=${OPTARG};;
            o) directory=${OPTARG};;
            r) RepSpec=${OPTARG};;
            t) ProcNum=${OPTARG};;
            f) Flank=${OPTARG};;
            b) both=${OPTARG};;
            h) usage; exit 1;; 
        esac
    done

echo "Species is $RepSpec"

case $RepSpec in
    "") stage="Running Initial Mask with Custom Library" ; firstCustom;;
    *) stage="Running Initial Mask with RepBase" ; firstMask;;
esac

When I run this script specifying the -r flag (which I am trying to make optional), the variable $RepSpec remains empty and this leads to the wrong command running in the second case statement. I am essentially trying to write a script where the -r flag is optional and if it is specified, then "firstmask" should run. If it is empty, then "firstCustom" should run.

I have tried:

altering getopts to :r: , :r , r: - $RepSpec remained empty unless r: is specified, but I am not totally sure why and does this mean that r is a required flag?

I also tried using an ifelse:

if [ -z "$RepSpec" ]; then
    firstCustom
else
    firstMask
fi

However I also couldn't get this to work, so I am assuming my problem lies somewhere in the getopts format?

Thanks in advance for any help!

toby_b
  • 1
  • 3

1 Answers1

0

I was simply missing : to ensure getopts is looking for an option argument

Updated to:

while getopts g:s:l:o:t:f:r:b:h: option

and now script is working

toby_b
  • 1
  • 3