Help with options and arguments to a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with options and arguments to a script
# 1  
Old 02-16-2009
Help with options and arguments to a script

I'm trying to write a script that accepts both arguments and options, e.g.
Code:
./script -h 1 -m 15 -s 30 [list of arguments]

or
Code:
./script [list of arguments] -h 1 -m 15 -s 30

I'd like for any of the arguments and options to be optional, and the option values should be numerals only. I've tried both getopt and getopts but I just can't seem to get it right at all. Help would be much appreciated.
# 2  
Old 02-16-2009
without using getopts, options are normally handled using a combination of a while and case loop:

Code:
while [ $# -gt 0]
do
    case $1 in
        <1st arg>)    <code/function to handle arg>; shift
        <2nd arg>)    <code/function to handle arg>; shift
        .
        .
        .
        *)    <code/function to handle default arg>...
    esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to iterate over several options

Have two 3 files which has list of servers,users and location and base url which is common on every server A = server1 server2 server3 B = user1 user2 user3 C = dom1 dom2 dom3 baseurl=/opt/SP/ and what i have to achieve is below via ssh from REMOTE SERVER for it's first iteration it... (7 Replies)
Discussion started by: abhaydas
7 Replies

2. Shell Programming and Scripting

Script variable help, Varying number of arguments to excute script

Hi Guy's. Hopefully someone can help me with what I am trying to archieve. So situation currently is, I have a script already setup however I have another script that sits infront of it. The main script basically goes and searchs multiple platforms for a list of entered data. In... (10 Replies)
Discussion started by: mutley2202
10 Replies

3. Shell Programming and Scripting

Processing Multiple Arguments in Command Line Options

Hi All, I am new to scripting. Could you please assist me . Here is my requirement. I have written a script that has 2 option flags defined. -l) calls some function with the arguments passed in front of -l -r) calls second function with the arguments passed in front of -r *) calls the... (7 Replies)
Discussion started by: Jay Deshpande
7 Replies

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

5. Shell Programming and Scripting

Intersperse arguments and options w/ getopts

Is it possible to get a script that uses getopts to accept options and arguments in any order? eg. -g -h 2 4 works like -g 2 -h 4. (1 Reply)
Discussion started by: lee.n.doan
1 Replies

6. UNIX for Dummies Questions & Answers

[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... (4 Replies)
Discussion started by: pinga123
4 Replies

7. Shell Programming and Scripting

Passing options into a script

Afternoon all, I have been writing a script to do some selects on a table dependent on what options are selected when the script is run: #!/bin/ksh set -x set -m if then echo "usage: msglog.ksh -da <date and time> -i <interface> -m <msg> -di <direction> -mi <MIR>" exit 1 fi... (3 Replies)
Discussion started by: chris01010
3 Replies

8. UNIX for Advanced & Expert Users

Options of setup someone to execute a script as my id.

Hi I would really appreciate your help: First I knew there are a few ways to do it: 1. Best is to set up sudoer...I'd like to see this as the last resort...there's a good reason for that ... 2. use setuid command, but that command needs to be a binary executable, while my command is a... (6 Replies)
Discussion started by: TheGunMan
6 Replies

9. Solaris

How to run a script with options

I have a script name as psin_install_i3fp.sh I need to run this script like ./psin_install_i3fp.sh step2 What this step2 represents? my script contains data: #!/bin/ksh mkdir logs >> /dev/null 2>&1 ./infra/bin/psin_stop_ba.sh mv ./psin_start_ba.sh... (2 Replies)
Discussion started by: pmrajesh21
2 Replies
Login or Register to Ask a Question