Adding -options to shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding -options to shell scripts
# 1  
Old 06-21-2005
Adding -options to shell scripts

I'm sure this is something simple I am overlooking somehow. I'd like the ability to pass -options into my shell scripts. For example my file called "input.sh" I can do the following:
root# ./input.sh 1

and it will result:
root#./input.sh 1
You passed me a 1

Just like an init script, etc.

I would like to be able to do:
root# ./input -m 1

and have the -m specify a different result in the case statement. Like:

root# ./input.sh -m 1
You passed me a -m 1

#! /bin/bash
case "$1" in
1)
echo "You passed me a 1"
;;
2)
echo "You passed me a 2"
;;
*)
commands;
;;
esac

Thanks for any suggestions as always guys. Smilie
# 2  
Old 06-21-2005
look into 'man getopt'
# 3  
Old 06-21-2005
Quote:
Originally Posted by vgersh99
look into 'man getopt'
Thank vgersh. This looks pretty gnarly. I will have to poke at it with a stick a bit and see if it moves. Smilie
# 4  
Old 06-21-2005
Actually its not that bad. On newer systems, try using getopts. Here is a link that provides an overview and examples for parsing options with case, getopt, and getopts
# 5  
Old 06-21-2005
Quote:
Originally Posted by google
Actually its not that bad. On newer systems, try using getopts. Here is a link that provides an overview and examples for parsing options with case, getopt, and getopts
Excellent addition google, thank you. Smilie

It does indeed seem much more complicated than it really is.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 ./script.sh -u username -p ***** -S processname there may... (5 Replies)
Discussion started by: atul9806
5 Replies

2. UNIX for Dummies Questions & Answers

List of 'if -f' options - AIX / Korn Shell

Hi all, Can someone point me in the right direction for a manual on the various statement options for 'if'. Basically I have a piece of code which says: if ] and I wondered what the -f was. I know the '!' means not equal.. As usual any help much appreciated.. (5 Replies)
Discussion started by: Grueben
5 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Scripts that take String options

I need help to know how to create a script that takes string options and not only 1 charecter. I wanted to make a script that takes 'string' options eg. hscript.sh -product XXX -version XXX I know that I can make a script that takes "1 Charecter " options eg. hscript.sh -m XXX -l XXX -s XXX ,... (2 Replies)
Discussion started by: hanhanbib
2 Replies

7. UNIX for Dummies Questions & Answers

Adding PATH variable in the startup scripts

Hi, I got a error message The environmental variable AUTOSYS is not set ! I think I require to to add a path to PATH variable in the startup scripts in the home dir. I tried using set PATH= but it is not setting the PATH when i do the echo $PATH , i dont get the new PATH reflected, Is... (5 Replies)
Discussion started by: rajanishshetty
5 Replies

8. UNIX for Dummies Questions & Answers

Options for csh shell

Hello, It's possible to change/put some options of the C-shell, like bold-text, date, etc? I can't found a web site that have a explanation. Thanks. (10 Replies)
Discussion started by: luiz_fer10
10 Replies
Login or Register to Ask a Question