[solved] Script creation (how to include options in the script)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [solved] Script creation (how to include options in the script)
# 1  
Old 07-15-2010
[solved] 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

# 2  
Old 07-15-2010
look at getopts.. (man page has clean usaeg examples..)
# 3  
Old 07-15-2010
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
#

# 4  
Old 07-15-2010
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";;
    esac
  done
fi

exit 0
#finis

Code:
[house@leonov] bash code.bash -a
Automatic Mode Selected
[house@leonov] bash code.bash -i
Interacive Mode Selected
[house@leonov] bash code.bash -f
Please select proper option
[house@leonov] bash code.bash
Automatic Mode Selected

This User Gave Thanks to dr.house For This Post:
# 5  
Old 07-15-2010
How do i mark the thread as solved?

Moderator's Comments:
Mod Comment Done.

Last edited by Scott; 07-15-2010 at 06:57 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to include menu based options in Shell script?

Hi Friends, I have a menu based tool which requires input/option to proceed further. How to make a shell script ? eg: menu looks like Get_data.sh to continue (y/n) : Here I need to key in "y" to proceed. I want to prepare a script which should consider option y. (5 Replies)
Discussion started by: suresh3566
5 Replies

2. Programming

[Solved] Cannot execute file that include files

Iam trying to execute a file that include many files but it seems my main copy.c can't read anyone of them ----------------------------------------------------------------------------------------- Copy.c #include <sys/stat.h> #include <fcntl.h> #include "tlpi_hdr.h" #ifndef BUF_SIZE /*... (2 Replies)
Discussion started by: fwrlfo
2 Replies

3. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

4. Shell Programming and Scripting

How to include SQL within a script

Hi, I have a script I am developing (actually more than 1 just now). 1st part - executes SQL in a 2nd script. 2nd part - reformats the output of the sql into a csv Problem: How am I able to run the SQL from the first script? I assume there is a sql command? I generally use isql when... (6 Replies)
Discussion started by: mcclunyboy
6 Replies

5. Shell Programming and Scripting

How to include a command in shell script?

# dbc "delete from alert;" DELETE 10 # However, the script created below generates an error that command does not exits. Can any one please exist. script.sh: #!/bin/sh dbc "delete from alert;" >>$TASKLOGFILE ./script.sh: line 38: dbc: command not found can any one please... (2 Replies)
Discussion started by: sureshcisco
2 Replies

6. UNIX for Dummies Questions & Answers

script/program in vi include $? or...

Hi. When you write a script/program in vi Do you include the $ Eg. #!/bin/bash $ echo "Today's date is `date`" or echo "Today's date is `date`" Make your script executable. $chmod +x script I included that. I keep getting a permission denied... why? Thanks! (5 Replies)
Discussion started by: JudoMan
5 Replies

7. Shell Programming and Scripting

Need help in a shell script to edit a tablespace creation script.

Hi, CREATE TABLESPACE aps_blob_large01 DATAFILE '/c2r6u13/u03/oradata/qnoldv01/aps_blob_large0101.dbf' SIZE X 270532608 REUSE DEFAULT STORAGE (INITIAL 134217728 NEXT... (2 Replies)
Discussion started by: rparavastu
2 Replies

8. Shell Programming and Scripting

include in script

Hello, I have a script and a second file that contains all parameters needed for the script. How can I include this file in the script ? I tried source but I have a "no such file or directory" error. :) (1 Reply)
Discussion started by: pppswing
1 Replies

9. UNIX for Dummies Questions & Answers

Include PERL script with in the unix shell script

Hi Gurus, Is it possible to include perl script with in the unix shell script? What would be the general syntax? In the above case, is it required to write the below first two lines of codes? #!usr/bin/sh -x #!usr/bin/perl -w Thanks in advance / Mysore Ganapati. (1 Reply)
Discussion started by: ganapati
1 Replies

10. Shell Programming and Scripting

Include lines within a script

I want to add lines in my script dynamically. My script: ... echo addscript end addscript ... Lines to add: (can be put in a file and can be added) cp /opt/script1.sh opt/script1_org.sh sed 's/testusr/'${FUNCTID}'/g' /opt/script1.sh > ./tmpfile mv ./tmpfile /opt/script1.sh... (1 Reply)
Discussion started by: chiru_h
1 Replies
Login or Register to Ask a Question