I am writing a script which will intercept any command I fire on my shell (ksh93), and process the options and arguments according to my requirement.
Say, I have script.ksh
./script.ksh
$ rm -rf dir1 dir2
Then I want to extract r,f and dir1, dir2 separately, so that I can examine each option individually and then do something more before deleting dir1, dir2
I came up with the following code to separate options and arguments into different arrays:
read INPUT
count=`echo $INPUT | wc -w`
echo $count
command=`echo $INPUT | cut -d' ' -f1`
echo $command
counter=2
indexOptions=0
indexArgs=0
while [ "$counter" -le "$count" ]
do
word=`echo $INPUT | cut -d' ' -f$counter`
if [[ "$word" == -* ]]
then
options["$indexOptions"]=$word
((indexOptions=indexOptions+1))
else
args["$indexArgs"]=$word
((indexArgs=indexArgs+1))
fi
((counter=counter+1))
done
But I wish to know if there is a better way or any other command or tool which I can use to improve my approach.
Thanks in advance.