LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script creation (how to include options in the script) (https://www.linuxquestions.org/questions/linux-newbie-8/script-creation-how-to-include-options-in-the-script-819911/)

pinga123 07-15-2010 01:54 AM

Script creation (how to include options in the script)
 
Hi guys i have written a script which takes the options given to him and execute itself accordingly.
for example
if a script name is doctortux then executing doctortux without option should made doctortux to be executed in automatic mode i.e. doctortux -a
or if a doctortux is needed to run in interactive mode we need to specify -i option.

To simulate above conditions i m using following code.
Is it the best practice to achieve the above mentioned goal?
Code:

if [ -z "$1" ] || [ "$1" == "-a" ]
then
echo "________________________________________________________________"
echo "Mode:Automatic use doctortux -i for Interactive mode"
MODE="Automatic"
echo "________________________________________________________________"
else
if [ ! -z "$1" ]
then
if [ "$1" ==  "-i" ]
then
echo "________________________________________________________________"
echo "Interactive mode selected:(Work in progress)"
MODE="Interactive"
echo "________________________________________________________________"
else
echo "$1 is not a Valid Option.Exiting from script.."
exit 1
fi
fi
fi


grail 07-15-2010 02:05 AM

So firstly, well done on using the code tags, but if you don't bother to indent it is almost as unreadable as not having used them at all.

As for best practice, probably horses for courses, but you could take a look at the getopts command in bash

pinga123 07-15-2010 02:47 AM

Quote:

Originally Posted by grail (Post 4033606)
So firstly, well done on using the code tags, but if you don't bother to indent it is almost as unreadable as not having used them at all.

As for best practice, probably horses for courses, but you could take a look at the getopts command in bash

The above code is modified as you said .However i have some doubt to be asked.
1)How would i consider automatic mode when no option is provided with the script name?
2)why "/usr/local/sbin/doctortuxv1: illegal option -- f" illegal option is said when i have given -f?

Code:

while getopts ai option
do
case "$option" in
a) echo "Automatic Mode Selected";;
i) echo "Interacive Mode Selected";;
[?]) echo "Please select proper option";;
esac
done

output:

Code:

# doctortuxv1 -a
Automatic Mode Selected
# doctortuxv1 -i
Interacive Mode Selected
# doctortuxv1 -f
/usr/local/sbin/doctortuxv1: illegal option -- f
Please select proper option
# doctortuxv1
#


grail 07-15-2010 03:34 AM

How about a simple if that tests to see if the first argument passed in ($1) has a dash (-).
If yes use getopts, if no then set default.

pinga123 07-15-2010 03:45 AM

Quote:

Originally Posted by grail (Post 4033663)
How about a simple if that tests to see if the first argument passed in ($1) has a dash (-).
If yes use getopts, if no then set default.

what if someone uses -- instead of -
as
Code:

man --help
man -help

yields same result.

konsolebox 07-15-2010 04:51 AM

in many shells, it's commonly done this way:
Code:

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


pinga123 07-15-2010 05:43 AM

Quote:

Originally Posted by konsolebox (Post 4033714)
in many shells, it's commonly done this way:
Code:

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


What about this?
Code:

#!/bin/bash

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


konsolebox 07-15-2010 05:56 AM

I also used getopts before and both are fine to me. It's ok to use whatever is comfortable or easy for you. I wonder but probably getopts is not available in all shells that are not bash. Not really important if it's only for bash.


All times are GMT -5. The time now is 01:12 PM.