Write a shell script with options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write a shell script with options
# 1  
Old 03-20-2012
Write a shell script with options

Hi All

I am little bit confused to write a script. This script needs the options like unix commands
i.e.

–S to start process.
–C to check process.
-u : user
-p : password

like.

script should run like this

Code:
./script.sh -u username -p ***** -S processname

there may be all the options or not. So I am confused how to write this script ??

My Idea :
I want to read all the argument pass and match with the respected details
like if there is -u used so next should be a username or -S then next should be a process name. But this thing needs lot of checks on the arguments only.
and after that I have to call the "case" in the script if there is any specific option in command line.

Please suggest your idea or help me with mine.

thanks
Atul Singh

Moderator's Comments:
Mod Comment Its square brakets for code tags... Smilie

Last edited by vbe; 03-20-2012 at 12:19 PM..
# 2  
Old 03-20-2012
What will it be doing if no argument sent?

Since you are the one writing the script dont accept no agument...and add -h for help...
Can S and C be used together?
how many processes can it "start"?
# 3  
Old 03-20-2012
thanks vbe your suggestions.

No, S and C is not use together. Here no option is used like other
Code:
ls -lrt

.

In my case if a option is there , its respective value should be there.

Thanks
Atul Singh
# 4  
Old 03-20-2012
Use a loop with a case statement:

Code:
MODE=""

# Loop until we run out of arguments.
while [ "$#" -gt 0 ]
do
        case "$1" in
        -p)
                shift
                MYPASS="$1"
                ;;
        -u)
                shift
                MYUSER="$1"
                ;;

        -S)
                MODE="start"
                ;;

        -C)
                MODE="check"
                ;;
        *)
                echo "Unknown argument '$1'" >&2
                exit 1
                ;;
        esac

        # Delete $1 argument, set $1=$2, $2=$3, ...
        shift
done

if [ -z "$MODE" ]
then
        echo "Must give -S or -C" >&2
        exit 1
fi

if [ "$MODE" = "check" ]
then
        echo "doing check"
fi

...

# 5  
Old 03-20-2012
Read about getopts. There're lot of posts in this forum on how to use getopts.
# 6  
Old 03-20-2012
thanks guys for your suggestions and ideas.
will read about getopts

thanks
Atul Singh
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. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. Shell Programming and Scripting

Automate the menu options using shell script

I have a menu option which will look as follows Select a menu option 1.change password 2.login as root user 3.show system version 4.quit Select> 1 please enter the new password: unix reenter the new password: unix press any key to enter (then displays again the menu options to enter the... (4 Replies)
Discussion started by: shivakumar6g
4 Replies

5. Shell Programming and Scripting

How to automate user selection options in shell script?

Hi There, I am trying to write a script which has to pick the prompted options by itself(i mean option to choose will be passed) here is real scenario i am trying to do. i have an executable(diagnos) which gets called in shell script, when the executable (diagnos) runs i get following as... (8 Replies)
Discussion started by: sairam_9191
8 Replies

6. Shell Programming and Scripting

shell script options

one thing i was trying to figure out is if you can give people the option to choose what they want to do in a shell script. for example, let's just say that you have a simple shell script to install a couple of programs, can you make it to where you can press a certain key to install a certain... (1 Reply)
Discussion started by: hotshot247
1 Replies

7. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

8. Shell Programming and Scripting

Help with shell script to run the commands reading options from local file

I have to use shell script to run series of commands on another unix box by connecting through SSH and giving user credentials. For running commands on remote machine I have to use options reading from a local file. Process: Connecting to remote unix server <host1.ibm.com> through ssh Login: ... (2 Replies)
Discussion started by: itsprout
2 Replies

9. Shell Programming and Scripting

Shell script to invoke options automatically

i have a script which has 2 options. a b And a has 6 sub options. i want to write a script which will call the parent script and give options automatically. examle: linasplg11:/opt/ss/kk/01.00/bin # startup.sh /opt/ss/rdm/01.00 Please select the component to... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

10. Shell Programming and Scripting

Adding options to a shell script

I want to add options to my shell script but having problems, my code so far is; #!/bin/bash lflag= iflag= while getopts 'l:i:' OPTION do case $OPTION in l) lflag=1 lval="$OPTARG" ;;... (1 Reply)
Discussion started by: paulobrad
1 Replies
Login or Register to Ask a Question