Reputation: 1053
I know "for loop" can be used to go through all arguments. But for my code, I would prefer
i = 1
if [ ${$i} == xx] ; then
xxx
fi
((iArg++))
if [ ${$i} == xx] ; then
xxx
fi
Oberviously, ${$i} is not working. How shall I make it correct?
I used to write csh by using
$argv[$i]
Got to use sh shell now. Thanks for help
Upvotes: 2
Views: 231
Reputation: 361564
Because of this very difficulty, argument parsing is typically done by using $1
and shift
. A typical loop might look like:
while [ $# -gt 0 ]; do
case $1 in
-a) echo "-a"; shift 1;; # option with no argument
-b) echo "-b $2"; shift 2;; # option which takes argument, use $2
-c) echo "-c"; shift 1;;
esac
done
That said, the way to do indirect variable names is with !
, as in:
${!i}
Upvotes: 5