LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2010, 02:54 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
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
 
Old 07-15-2010, 03:05 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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
 
Old 07-15-2010, 03:47 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
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
#

Last edited by pinga123; 07-15-2010 at 04:03 AM.
 
Old 07-15-2010, 04:34 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
Old 07-15-2010, 04:45 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
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.
 
Old 07-15-2010, 05:51 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
 
Old 07-15-2010, 06:43 AM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by konsolebox View Post
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
 
Old 07-15-2010, 06:56 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User Creation Script mfaisalkh Programming 1 08-06-2007 04:57 AM
mysql user creation script tanveer Linux - Server 2 06-20-2007 02:21 AM
script creation woes mrgreaper Linux - Newbie 10 06-20-2006 08:41 AM
include a file in perl script Ateo Programming 1 04-22-2005 10:53 PM
Perl Script include mikeshn Programming 1 08-20-2002 10:55 AM


All times are GMT -5. The time now is 04:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration