Writing Unix script to accept arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing Unix script to accept arguments
# 1  
Old 05-26-2009
Writing Unix script to accept arguments

Hi,

This may be answered elsewhere but I wasn't entirely sure of the wording I should use to search so here we go with an attempt:

I wish to make a script that will allow commands to be passed to it such as:

<command> -oOPTIONS -aANOTHER -pRINT

etc

However I don't really know the syntax of what to do ir how to accept them with in the code.

Like for example:

If I want it do run a certain command if -o equals "Fred".

If anyone has any help or "how to's" it would be most apprieciated.
# 2  
Old 05-26-2009
Take a look at getopts (about 3/4 down the page)
# 3  
Old 05-27-2009
Another simple example in bash (if you do not care about the switches' names) :
Code:
#!/bin/bash

parseArguments() {
    if [[ $# -lt 3 ]]; then
        echo "Usage: $0 host user pass"
        exit 1
    fi
host="$1"
user="$2"
pass="$3"

# do you stuff here, and put the following at the main part of the script : 
parseArguments() $@

The $0 is the name of the script.
# 4  
Old 05-27-2009
In pure bourne shell...

Code:
f=0
while [ $# -gt 0 ]; do
        case "$1" in
        -o?*)
                # handles things like -oValue
                o=`expr "$1" : '..\(.*\)'`
        ;;
        -o)
                # handles things like -o Value
                o="$2"
                shift
        ;;
        -f)
                # Just a flag (on/off)
                f=1
        ;;
        *)
                break
        ;;
        esac
        shift
done
echo "o=$o, f=$f"
# Loop through remaining arguments (arguments without a hyphen)
for arg in "$@"; do
        echo "$arg"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX script can accept 1 to n parameters

Just for my leaning purpose, I appreciate if someone answer my question: A UNIX script can accept 1 to n parameters. For each of these parameters, write out the parameter id number and its value. (1 Reply)
Discussion started by: shumail
1 Replies

2. Homework & Coursework Questions

Make a file accept only two arguments from the command line

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) The script is executed in the Korn shell. 2) Name the shell script file is asg6s. 3) The asg6s file is... (7 Replies)
Discussion started by: ProgMan2015
7 Replies

3. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

4. Programming

Writing C++ program Arguments and Classes

I want to write a C++ program that uses a class to do some calculations. I pass arguments to the program, some of which are used to set up class members. A class function will then perform the necessary calculations. I am wondering how I should pass the arguments from the program to set the... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

How to accept arguments in shell script when calling in perl

I have a shell script like this: #!/bin/sh $PYTHON MetarDecoder.py < ../data/mtrs/arg1/arg2 And I'm calling it with this in perl: my $output = `./metar_parse.sh --options`; It's successful when I put in actual values for arg1 and arg2 in the shell script, but I'd like to pass arguments... (1 Reply)
Discussion started by: civilsurfer
1 Replies

6. UNIX for Dummies Questions & Answers

script unix which accept two arguments

does anyone can help me with this homework, please..I am beginner in linux and I don't how to do it :( Create a script scanner.sh which will accept two arguments: the first argument is the DNS name or the IP address of a system, or a network address or an IP range, the second argument is... (1 Reply)
Discussion started by: gennyy
1 Replies

7. Shell Programming and Scripting

ksh script that will accept arguments

Hi, I am not very skilled using ksh scripts. How do I create a ksh script that will accept arguments and use them in the script ? I need to make this: Run this command with this argument: ./mykshprogram.ksh madsen and sometimes I need to do this: Run the ksh again with 2... (3 Replies)
Discussion started by: hasselhaven
3 Replies

8. Shell Programming and Scripting

need help writing this unix script

Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the file exists, prompt the user for confirmation before renaming the file. The screen should prompt the user for a. "Name of file you want to rename." Use the "\c"... (7 Replies)
Discussion started by: wiggles
7 Replies

9. Shell Programming and Scripting

help writing this unix script

I am working on writing scripts. Here is a script I need help with. I have also wrote what I think it is. I would really appreciate any help that I can get. Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the... (2 Replies)
Discussion started by: wiggles
2 Replies

10. Shell Programming and Scripting

Need help in writing a unix script

OS: Solaris Shell : KSH Please help me in writing a script that captures a error message from a log file ( which updates continiously ) and send an email alert as soon as the systems throws a error message into that log. i.e With out monitoring the log Thanks in advance.. (1 Reply)
Discussion started by: pray44u
1 Replies
Login or Register to Ask a Question