I'm pretty new to shell scripts, so it confuses me a little and I couldn't seem to find a solution to this. Let's say I have a shell script that can take a number of arguments. For the sake of example, I could call it as follow:
myscript -a valA -b valB -c valC -d valD some/directory
Now, some of these arguments are for my script itself, while others are for a command that will be called within my script. So for this case, -a, -d and the directory are for my script, everything else will be for the command. So I want to do something like this:
args=''
if [ $# == 0 ]
then
echo "No arguments found!"
exit 1
fi
while [ "$2" ]
do
if [ $1 == '-a' ]
then
#some process here
shift 2
elif [ $1 == '-d' ]
then
#some process here
shift 2
else
#add the argument to args
shift
fi
done
directory=$1
for file in $directory/*.txt
do
#call 'someCommand' here with arguments stored in args + $file
done
I've tried doing
args="$args $1"
Then calling the command doing
someCommand "$args $file"
but then, someCommand seems to think the whole thing is one single argument.
Also, if you see anything wrong with the rest of my script, feel free to point it out. It seems to work, but I could very well be missing some corner cases or doing things that may lead to unexpected behaviors.
Thanks!
if [ $# == 0 ]clause. Instead, just write:directory=${1?No directory specified}when you make the assignment to directory.if/elif/elsechain with acasestatement.