Skip to main content
deleted 7 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 29
  • 156
  • 240

I'm new to using this :

for example ./imgSorter.sh -d directory -f format

the scripts' content is :

#!/bin/bash while getopts ":d:f:" opt; do case $opt in d) echo "-d was triggered with $OPTARG" >&2 ;; f) echo "-f was triggered with $OPTARG" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done 

use cases :

$ ./imgSorter.sh -d myDir -d was triggered with myDir OK

$ ./imgSorter.sh -d -f myFormat -d was triggered with -f NOK : how is it that a string beginning with - is not detected as a flag ?

I'm new to using this :

for example ./imgSorter.sh -d directory -f format

the scripts' content is :

#!/bin/bash while getopts ":d:f:" opt; do case $opt in d) echo "-d was triggered with $OPTARG" >&2 ;; f) echo "-f was triggered with $OPTARG" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done 

use cases :

$ ./imgSorter.sh -d myDir -d was triggered with myDir OK

$ ./imgSorter.sh -d -f myFormat -d was triggered with -f NOK : how is it that a string beginning with - is not detected as a flag ?

I'm using this :

for example ./imgSorter.sh -d directory -f format

the scripts' content is :

#!/bin/bash while getopts ":d:f:" opt; do case $opt in d) echo "-d was triggered with $OPTARG" >&2 ;; f) echo "-f was triggered with $OPTARG" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done 

use cases :

$ ./imgSorter.sh -d myDir -d was triggered with myDir OK

$ ./imgSorter.sh -d -f myFormat -d was triggered with -f NOK : how is it that a string beginning with - is not detected as a flag ?

Source Link
St3an
  • 123
  • 1
  • 1
  • 7

how to properly parse shell script flags and arguments using getopts

I'm new to using this :

for example ./imgSorter.sh -d directory -f format

the scripts' content is :

#!/bin/bash while getopts ":d:f:" opt; do case $opt in d) echo "-d was triggered with $OPTARG" >&2 ;; f) echo "-f was triggered with $OPTARG" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done 

use cases :

$ ./imgSorter.sh -d myDir -d was triggered with myDir OK

$ ./imgSorter.sh -d -f myFormat -d was triggered with -f NOK : how is it that a string beginning with - is not detected as a flag ?