user2141414
user2141414

Reputation: 101

UNIX basic getopts trouble

I have just recently started using UNIX and having problems trying to implement getopts.

The function below finds a file and then removes it to the recycle bin, tho I'm trying to use getopts with -i that will display a message after it has been moved. The syntax works fine, but when I implement the while loop with the getotps command it no longer works.

Can anybody give me any useful advice, it would be much appreciated

function moveToBin(){

while getopts i opt
do
   case $opt in
   i) echo "file removed!" ;;
esac
done

if [[ -e $1 ]]; then
   inode=$(ls -i  $i | cut -d " " -f1)
   name=$1_$inode
   pathOfFile=$(pwd $1)
   restoreEntry=$1_$inode:$pathOfFile/$1

        mv $1 ~/deleted
        mv ~/deleted/$1 ~/deleted/$name
            echo "Before extension code"
               extension=$(find ~ -inum $inode)
 fi

Upvotes: 1

Views: 153

Answers (1)

linux_fanatic
linux_fanatic

Reputation: 5167

while getopts ...; do
  ...
done

getopts will parse options and their possible arguments. It will stop parsing on the first non-option argument (a string that doesn't begin with a hyphen (-) that isn't an argument for any option in front of it). It will also stop parsing when it sees the -- (double-hyphen), which means end of options.

Upvotes: 1

Related Questions