looping while using getopts

so when i use rm file -i vv bb i can delete the first file vv but i cant seem to delete the second file bb as i tried to use shift on it, but it seems that it delete the first one and stops it without looping it to the next file. what is wrong with my coding ?


#!/bin/bash

function removei () {



clear

if [[ -d $file ]]; # -d allows to check if the value is a directory
     then
                                echo -e "are you sure you want to remove this directory (y/n)"
                                        read ANSWER


                case "$ANSWER" in 

                                        [Yy]) echo "the directory $file has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift
                                                    ;;

                                        [Nn]) echo -e "exiting"
                                                       exit
                                                       ;;


                                        *) echo "Answer y or n"
                                                ;;

                 esac

elif [[ -f $file ]]; # -f allows to check if the value is a file
        then
                               echo "are you sure you want to remove the file (y/n)"

                                   read  ANSWER1
case "$ANSWER1" in


                                        [Yy]) echo "the file $file  has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift
                                              echo "the file $file  has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift

                                                    ;;

                                        [Nn]) echo "exiting"
                                                    exit;;


                                        *) echo "Answer y or n";;

                esac

else
        echo "$file does not exist" 
fi


}

file=$2

while getopts "ivfrR:" option



do

case $option in

        i) removei;;

        v) removev;;

        f) removef;;

esac

shift 1

done

The first thing I noticed was that you set file outside of your while loop, so it will always have the same value.

I'm not a heavy getopts users, but it seems that shifting in the getopts loop is wrong. My impression is that the way getopts is intended to be used is to 'gather' information from command line options, and parse positional parameters outside of that loop. Something like this:

function removei
{
    echo "removei: $f"
}

method="i"                # default method should the script be invoked without flags
while getopts "ivr" flag     # set method based on the command line options
do
    case $flag in
        i)  method=i;;
        r)  method=r;;
        v)  method=v;;
    esac
done

shift $(( ${OPTIND:-1} - 1 ))   # shift away 'used' flags and flag/data
while [[ -n $1 ]]   # until there are no more positional parameters
do
    f=$1              # assign file name each pass of the loop
    case $method in    # invoke method selected by -i|-v|-r above for current file
        i)  removei;;
        v)  echo "removev";;
        r)  echo "removev";;
    esac

    shift      # shift away last file/directory worked on 
done

exit

ok the coding that you gave me works however, on the

function removei
{
    echo "removei: $f"
}

i wanted to add the validation bits that i showed on the first comment where it will check if its a file or directory and deletes it then go to the next file, however it still doesnt shift it when i put in the code of that, if you know what i mean.

If you are trying to shift in the function, it's not going to do what you think as only one filename per call is being passed. The shift will need to take place in the second loop. If you are shifting in the second loop and it's not working it's difficult to say what is going on without seeing your script.