0

I'm trying to make a script which starts with the shebang #!/bin/sh, and which, amongst other things, loops through the script's arguments, in order to check whether a given argument is present.

Here's what I've come up with so far:

#!/bin/sh

mother_flag="n"
virgin_flag="n"
reset_flag="n"

i=0
while [ $i -le $# ]
do
    echo "${i}" # <<< This line isn't doing what I want it to do!
    if [ ${i} = "--mother" ]; then
        mother_flag="y"
    elif [ ${i} = "--virgin" ]; then
        virgin_flag="y"
    elif [ ${i} = "--reset" ]; then
        reset_flag="y"
    fi
    i=$((i+1))
done

echo "mother_flag = $mother_flag"
echo "virgin_flag = $virgin_flag"
echo "reset_flag = $reset_flag"

This runs, but it doesn't do what I want it to do. If I run sh my_script --mother, ${i} gives 0 and then 1. What should I put to make ${?} (or its replacement) give my_script and then --mother? Or is this impossible in shell script?

Or is there a better way of checking through one's arguments in shell script?

Tom Hosker
  • 526
  • 2
  • 17

0 Answers0