I want to create a script with some optional inputs parsed similar to how this respone explains but also containing a list of files with a certain extension. Example usage would be
./myscript.sh -s 25 -q 0.2 *.txt
and the script would then store 25 into the s
variable, 0.2 into the q
varable, and the list of all the txt files would then get individually processed. This is what I have so far
#!/bin/bash -f
# set the default values
QMAX=0.2
PROFILESIZE=50
while [[ $# > 0 ]]
do
key="$1"
case $key in
-q|--max_q)
QMAX="$2"
shift # past argument
;;
-s|--profile_size)
PROFILESIZE="$2"
shift # past argument
;;
esac
shift # past argument or value
done
for var in "$@"
do
if [[ $var == *".txt" ]]
then
# do something on for real on each file here
echo $QMAX $PROFILESIZE $var
fi
done
As is, I can run with the default values by commenting out the while
loop. Without commenting it out, it reads through the inputs and there are none left for the for
loop to compare. I think the best option is to create a list of the files in the while
loop, then use that in the for
loop. Any ideas of how I would do that?