0

I am new using unix and I recently tried to create a code that would take some arguments and pass it to a python script and later use those arguments for downstream processes.

I am trying to use getopts but after I tried running the script it looks like my arguments were not stored in variables and anything downstream failed.

This is a piece of what I tried so far

#!/bin/bash
function usage() {
    cat <<USAGE

    Usage: $0 [-f fasta] [-o output name] [-m memory] [-t time] [-n number of tasks] [-c cores] [-g gpus] [-e extra]

    Options:
        -f  fasta file
        -o  output name
        -m  memory to request
        -t  time to request
        -n  number of tasks
        -c  cores to request
        -g  gpus to request - no more than 4!
        -e  extra arguments for program
USAGE
}

while getopts ":f:o:m:t:n:c:g:e:h" flag
do
    case "${flag}" in
        f)
          fasta=${OPTARG}
            ;;
        o)
          out_name=${OPTARG}
            ;;
        m)
          memory=${OPTARG}
            ;;
        t)
          time=${OPTARG}
            ;;
        n)
          number=${OPTARG}
            ;;
        c)
          cores=${OPTARG}
            ;;
        g)
          gpus=${OPTARG}
            ;;
        e)
          extra=${OPTARG}
            ;;
        h)
          usage
            ;;
        :)
          echo "Invalid option: $OPTARG" 1>&2
          usage
          exit 1
            ;;
        \?)
            echo "Invalid option"
            usage
            exit 1
            ;;
    esac
done

python3 /scratch/amolinav/programs/my_script_single.py ${fasta} ${out_name} ${memory} ${time} ${number} ${cores} ${gpus} ${extra}

I would really appreciate if someone could point out where I am making the mistake. Thank you so much in advance for your help

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • Your script looks ok-ish - check it with shellcheck.net to correct some quoting problems. But it looks fine. `it looks like` What exactly "looks like"? Could you show us, based on what do you make the assumption, that the arguments were not stored? Add `set -x` to the second line of the script and re-run it - debug it. – KamilCuk Nov 02 '21 at 16:22
  • You shall protect the variables with double-quotes when using their content (ex `fasta="$OPTARG"` or `.../my_script_single.py "$fasta" "$out_name" ...`). Also, `getopts` is tricky as it stops to iterate through the arguments when encountering a _non-option_ (ex. `./myscript.sh whatever -f file.fasta` will not populate the `fasta="$OPTARG"`, but `./myscript.sh -f file.fasta whatever` will) – Fravadona Nov 02 '21 at 16:28
  • @KamilCuk Since the code failed, I tried to echo the variables at the end (e.g. `echo "${fasta}"`) and I got back an empty line. And thanks for pointing out the quoting problems, I corrected that – Adrian Molina Nov 02 '21 at 16:39
  • @Fravadona thank you for your answer. I corrected the quotation but my variables are still not getting populated and I can't find the reason why – Adrian Molina Nov 02 '21 at 17:49
  • So, How are you calling the program? I tested your program https://replit.com/@kamilcukrowski/LooseTreasuredSeptagon#main.sh works fine for me. What else can you tell us? – KamilCuk Nov 02 '21 at 18:57

0 Answers0