I have the following code, and I am changing it to
#!/bin/bash
hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0
OLDIFS="$IFS"
IFS="|=" # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $* # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"
while [ "$#" -gt 0 ]
do
case "$1" in
"--"[cC][mM][oO][dD][iI][fF]|\
"--"[cC][mM][oO][dD]"-"[iI][nN][fF][iI][lL][eE])
shift
arg_cmodInfile="${1}"
hasArgumentCModInfile=1
;;
"--"[sS][rR][cC][sS][iI][fF]|\
"--"[sS][rR][cC][sS]"-"[iI][nN][fF][iI][lL][eE]|\
"--"[sS][oO][uU][rR][cC][eE][sS]"-"[iI][nN][fF][iI][lL][eE])
shift
arg_srcsInfile="${1}"
hasArgumentSrcsInfile=1
;;
"--"[rR][cC][vV][sS][iI][fF]|\
"--"[rR][cC][vV][sS]"-"[iI][nN][fF][iI][lL][eE]|\
"--"[rR][eE][cC][eE][iI][vV][eE][rR][sS]"-"[iI][nN][fF][iI][lL][eE])
shift
arg_rcvsInfile="${1}"
hasArgumentRcvsInfile=1
;;
*)
arg_browseDir_fileLst="$arg_browseDir_fileLst ${1}"
hasArgumentBDirFileLst=1
;;
esac
shift # Skip ahead to the next argument
done
if [ $hasArgumentCModInfile -eq 1 ]; then
...
elif [ $hasArgumentCModInfile -eq 0 ];
...
fi
if [ $hasArgumentSrcsInfile -eq 1 ]; then
...
elif [ $hasArgumentSrcsInfile -eq 0 ];
...
fi
if [ $hasArgumentRcvsInfile -eq 1 ]; then
...
elif [ $hasArgumentRcvsInfile -eq 0 ];
...
fi
Basically I cut out the initializations in the beginning such as
hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0
Also I would have a common array called hasArgument with the field only filled when the corresponding option is selected.
I want to simplify it using associative arrays for storing the options as follows:
#!/bin/bash
OLDIFS="$IFS"
IFS="|=" # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $* # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"
while [ "$#" -gt 0 ]
do
case "$1" in
"--"[cC][mM][oO][dD][iI][fF]|\
"--"[cC][mM][oO][dD]"-"[iI][nN][fF][iI][lL][eE])
shift
value[cmodInfile]="${1}"
hasArgument[cmodInfile]=true
;;
"--"[sS][rR][cC][sS][iI][fF]|\
"--"[sS][rR][cC][sS]"-"[iI][nN][fF][iI][lL][eE]|\
"--"[sS][oO][uU][rR][cC][eE][sS]"-"[iI][nN][fF][iI][lL][eE])
shift
value[srcsInfile]="${1}"
hasArgument[srcsInfile]=true
;;
"--"[rR][cC][vV][sS][iI][fF]|\
"--"[rR][cC][vV][sS]"-"[iI][nN][fF][iI][lL][eE]|\
"--"[rR][eE][cC][eE][iI][vV][eE][rR][sS]"-"[iI][nN][fF][iI][lL][eE])
shift
value[rcvsInfile]="${1}"
hasArgument[rcvsInfile]=true
;;
*)
value[bdFileLst]="${value[bdFileLst]} ${1}"
hasArgument[bdFileLst]=true
;;
esac
shift # Skip ahead to the next argument
done
if ${hasArgument[CModInfile]}; then # User set value for CModInfile
...
elif ! ${hasArgument[CModInfile]}; then # User did not select CModInfile option
...
fi
if ${hasArgument[SrcsInfile]}; then
...
elif ! ${hasArgument[SrcsInfile]}; then
...
fi
if $hasArgument[RcvsInfile]}; then
...
elif ! ${hasArgument[RcvsInfile]}; then
...
fi
I would be very grateful on some comments concerning this scheme, whether it is a good idea or further improvements.