The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 03-18-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by naminator View Post
Thanks for the help but,

If you have to enter at least two arguments like cleandisk -I -V and if you type only one it gives some message.

Another thing if i type the dir i want after the arguments like:

cleandisk -I -V /home

how can i save the "/home" to a variable ?

It already is in a potitional parameter: $3

After processing the options, it will be in $1.

Code:
opts=IV ## Put the option letters you want to use in $opts
ok=0
while getopts "$opts" opt
do
  case $opt in
    I) echo option I; ok=$(( ok + 1 )) ;;
    V) echo option V; ok=$(( ok + 1 )) ;;
  esac
done
shift $(( $OPTIND - 1 )) ## remove options; /home will now be $1

if [ $ok -eq 0 ]
then
   echo You did not give any options >&2
   exit 1
else
   echo You entered $ok options
fi

if [ $# -gt 0 ]
then
   echo "The remaining arguments are:"
   printf "  %s\n" "$@"
else
   echo "There are no arguments"
fi