Search This Blog

Thursday, July 15, 2010

parsing command line option .

I have decided to run the script in two mode
1)Interactive.
2)Automatic.

Automatic mode ( -a ) (default):This mode shoudnt ask any input from user and should run automatically.

Interactive mode (-i) : This mode should ask user for input .


Initially i m trying to concentrate on automatic mode which is by default.



I have used getopts method to deal with command line arguments. However this might restricted to bash script because of getopts method.
Method used in program.
if [ $# -eq 0 ]
then
echo "Automatic Mode Selected"
else
while getopts ai option 2>/dev/null
do
case "$option" in
a) echo "Automatic Mode Selected";;
i) echo "Interacive Mode Selected";;
?) echo "Please select proper option"
echo list of available options
-a automatic -i interactive ;;
esac
done
fi

However I will also post universal method to establish the same task.

while [ "$#" -gt 0 ]; do
case "$1" in
-o|--long-option)
: do something
;;
-p|--option-with-arg)
: do something with $2
shift
;;
*)
# this could be an invalid option or just a file
# if no file is expected, do something like:
echo "invalid option or argument: $1"
;;
esac
shift
done

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

No comments:

Post a Comment