
03-18-2009
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
|
|
Quote:
Originally Posted by naminator
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
|