Visit Our UNIX and Linux User Community


Scripts that take String options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripts that take String options
# 1  
Old 10-18-2007
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 , with a function called "getopts".

When i tried to use "getopts" to make the options as a string and not only 1charecter the script didn't work.
# 2  
Old 10-18-2007
Try using a while loop to process the parameters, e.g...
Code:
while [[ $# -gt 0 ]]
do
    case $1 in
        -product) export PRODUCT=$2 ; shift ;;
        -version) export VERSION=$2 ; shift ;;
               *) echo invalid option ; exit 1 ;;
    esac
    shift
done

Note that options with arguments require additional shifts.
# 3  
Old 10-19-2007
Hi.

I think Ygor's suggestion is relatively easy to implement and useful to know about.

However, there are those extra actions with which getopts-like facilities can help us -- error messages, shifting, etc.

There is a shell version of getopt that can process long options. See Bash long options and Python s-expression parser for details ... cheers, drl

Last edited by drl; 10-19-2007 at 10:09 AM.. Reason: Clarify meaning.

Previous Thread | Next Thread
Test Your Knowledge in Computers #1011
Difficulty: Easy
The number 128 in base 10 equals 10000000 in base 2.
True or False?

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Best way to parse 1000 scripts for a string?

I have inherited code from a former employee and I need to identify the scripts he disabled with exit 0 at the top. After *many* trials and errors, I finally got this to work alan@p33 => find . -name "*.ksh" -exec sh -c "head -v -n2 '{}' | tail -v -n 1 | grep -H '^exit 0'" \; (standard... (7 Replies)
Discussion started by: alan
7 Replies

2. Shell Programming and Scripting

Problem in Concatination of string in bash scripts containing back slashes.

My script is as follows: #!/bin/bash STR1="test" echo $STR1 STR2="/bldtmp/"$STR1 echo $STR2 STR3=$STR2'/tmp' echo $STR3 output i am geting ---------------- test /bldtmp/test /tmptmp/test but my need is: ------------------ test /bldtmp/test (1 Reply)
Discussion started by: dchoudhury
1 Replies

3. Shell Programming and Scripting

How to make sure the input string is one of many options?

How to make sure the input string is one of many options e.g centos-5.5-i386 windows-2003r2-x64 ? The options are dynamic, so "case" condition check doesn't work. I use grep -o -w , it doesn't work every time because - is valid word boundry #word windows-2003 failed the check as expected... (4 Replies)
Discussion started by: honglus
4 Replies

4. Ubuntu

Kernel boot options removed by fault, no boot options

Hello Everyone, First of all, I highly appreciate all Linux forum members and whole Linux community. http://forums.linuxmint.com/images/smilies/icon_wink.gif. I wish you the best for all of you ! I will try to be short and concise: I am using Linux Mint 10 for 2 months on 2 ws, and all went... (3 Replies)
Discussion started by: cdt
3 Replies

5. Shell Programming and Scripting

how to? launch command with string of command line options

my description from another thread... here's my code: #!/bin/bash IFS=$'\n' function OutputName() { input=$1 echo $input input=`echo "$input" | sed -e 's/.//'` input=`echo "$input".avi` output_name=$input } if ]; then echo... (5 Replies)
Discussion started by: TinCanFury
5 Replies

6. Shell Programming and Scripting

How to hold string array in shell scripts

Gents, Below is the Shell script which I am trying to hold a string of array that is passed from a java file. But it is not working . Can any one please help me to by fixing it. #!/bin/csh/ set copy = ($argv) echo $copy >> /home/users/bavananr/rrr.log echo $copy >>... (3 Replies)
Discussion started by: brajesh
3 Replies

7. Shell Programming and Scripting

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

Featured Tech Videos