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
how to? launch command with string of command line options TinCanFury Shell Programming and Scripting 5 04-28-2008 07:06 PM
How to hold string array in shell scripts brajesh Shell Programming and Scripting 3 03-27-2007 10:14 AM
Adding -options to shell scripts sysera Shell Programming and Scripting 4 06-21-2005 11:56 AM
cat and wc options Laura01 Shell Programming and Scripting 1 09-09-2002 12:21 AM
cp options milage UNIX for Dummies Questions & Answers 3 07-13-2001 01:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-18-2007
hanhanbib hanhanbib is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 1
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 (permalink)  
Old 10-18-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
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 (permalink)  
Old 10-19-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 711
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 09:09 AM.. Reason: Clarify meaning.
Closed Thread

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 03:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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