The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help writing this unix script wiggles Shell Programming and Scripting 7 12-12-2008 09:15 PM
help writing this unix script wiggles Shell Programming and Scripting 2 12-12-2008 12:28 PM
Perl Unix Script Writing Dinkster UNIX for Dummies Questions & Answers 1 02-01-2008 04:09 PM
Writing and executing a script in RTR implementation of UNIX mahajan.anubhav Shell Programming and Scripting 0 03-16-2006 05:20 AM
Need help in writing a unix script pray44u Shell Programming and Scripting 1 03-30-2005 07:15 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-26-2009
stuaz stuaz is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 12
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 (permalink)  
Old 05-26-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,789
Take a look at getopts (about 3/4 down the page)
  #3 (permalink)  
Old 05-27-2009
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
  
 

Join Date: Nov 2006
Location: Bulgaria
Posts: 1,318
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 (permalink)  
Old 05-27-2009
cjcox cjcox is offline
Registered User
  
 

Join Date: May 2005
Posts: 64
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
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0