Parsing optional and not optional arguments
04:18 05 Jan 2022

I am new with bash and after reading and trying a lot about how to parse arguments I cannot what I really want to do I want to parse optional and not optional arguments. More specifically I want to parse 3 arguments, first (a fastaq file) second (a second optional fastaq file) a third argument that will be a directory.

my_script.sh -f1 file1.fasta --f2 file2.fasta -d/home/folder1/folder2 or

my_script.sh -f1 file1.fasta -d /home/folder1/folder2

I have tried to do this in many ways but I dont know how to let the program identifies when there are two fasta files and a directory and, when there is only one fasta file and a directory.

With this arguments I want to save them in variables because they will be used later by third parties.

I have tried this:



for i in "$@"; do
 case $i in
   -f1=|-fasta1=)
     FASTA1="${i#=}"
     shift # past argument=value
     ;;
   -d) DIRECTORY=$2
  shift 2
     ;;
   -d=|-directory=) DIRECTORY="${i#=}"
   shift # past argument=value
     ;;
   --f2=|-fasta2=) FASTA2="${i#*=}"
    shift # past argument=value
     ;;
   *)
     ;;
 esac
done

But I just got this

scripts_my_first_NGS]$ ./run.sh -f1 fasta.fasta -d /home/folder1
FASTA1  =
DIRECTORY     =
FASTA2     =
bash parsing