Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

getopt

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-07-2006
Hitori's Unix or Linux Image
Hitori Hitori is offline Forum Advisor  
kharkovpromenade.com.ua
 
Join Date: Jun 2006
Last Activity: 10 December 2011, 9:33 AM EST
Posts: 360
Thanks: 0
Thanked 9 Times in 8 Posts
getopt


Code:
#!/bin/sh

set -- `getopt "abco:" "$@"`

a= b= c= o=
while :
do
    case "$1" in
    -a) a=1;;
    -b) b=1;;
    -c) c=1;;
    -o) shift; o="$1";;
    --) break;;
    esac
shift
done
shift # get rid of --
# rest of script...
# e.g.
ls -l $@

This code works well until there are filenames with spaces, even getopt -s sh doesn't help. How this problem could be resolved?
Sponsored Links
    #2  
Old Unix and Linux 07-07-2006
grumpf grumpf is offline
Registered User
 
Join Date: May 2006
Last Activity: 24 October 2008, 11:54 AM EDT
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
space are evil. the easy way to to prevent them by quoting on the commandline.
Sponsored Links
    #3  
Old Unix and Linux 07-07-2006
Hitori's Unix or Linux Image
Hitori Hitori is offline Forum Advisor  
kharkovpromenade.com.ua
 
Join Date: Jun 2006
Last Activity: 10 December 2011, 9:33 AM EST
Posts: 360
Thanks: 0
Thanked 9 Times in 8 Posts
This wil not help as far as quoting will be removed by getopt
    #4  
Old Unix and Linux 07-08-2006
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 1 July 2015, 8:32 AM EDT
Posts: 310
Thanks: 4
Thanked 1 Time in 1 Post
hi all...


i have come across the posting ....
can any one please explain the code..

set -- `getopt "abco:" "$@"`

a= b= c= o=
while :
do
case "$1" in
-a) a=1;;
-b) b=1;;
-c) c=1;;
-o) shift; o="$1";;
--) break;;
esac
shift
done
shift # get rid of --
# rest of script...
# e.g.
ls -l $@

.....


what will set -- `getopt "abco:" "$@"` do and so on.
Sponsored Links
    #5  
Old Unix and Linux 07-08-2006
Hitori's Unix or Linux Image
Hitori Hitori is offline Forum Advisor  
kharkovpromenade.com.ua
 
Join Date: Jun 2006
Last Activity: 10 December 2011, 9:33 AM EST
Posts: 360
Thanks: 0
Thanked 9 Times in 8 Posts
Quote:
Originally Posted by arunkumar_mca
hi all...


i have come across the posting ....
can any one please explain the code..
$@ is all positional parameters, i.e. if you execute command attr0 attr1 then $@ is equal to attr0 attr1

The Bourne Shell set command sets positional parameters to the getopt's output, i.e. if getopt returns something like -a -b -- test then $@ will be equal to -a -b -- test

Note: '--' is used in set command to resolve problems with parameters that begins with '-' character

In while loop positional parameters are being processed till '--' ($1 refers to first positional parameter, shift deletes first positional parameter)
Sponsored Links
    #6  
Old Unix and Linux 07-10-2006
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 1 July 2015, 8:32 AM EDT
Posts: 310
Thanks: 4
Thanked 1 Time in 1 Post
Thanks a lot....
Sponsored Links
    #7  
Old Unix and Linux 07-10-2006
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
With KSH you can use getopts :

Code:
#!/bin/ksh

while getopts abco: name
do
   case $name in
      a)  a=1;;
      b)  b=1;;
      c)  c=1;;
      o)  o="$OPTARG";;
   esac
done

shift $(($OPTIND -1))
# rest of script...
# e.g.
ls -l $@


jean-Pierre.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
getopt in CSH animesharma Shell Programming and Scripting 4 07-26-2011 03:20 AM
Help with Getopt::Std Breakology Shell Programming and Scripting 3 06-23-2009 09:33 PM
getopt help darshakraut Shell Programming and Scripting 3 06-20-2008 08:15 AM
getopt help darshakraut Shell Programming and Scripting 2 06-18-2008 07:43 AM
getopt help problems Shell Programming and Scripting 1 05-02-2006 04:23 AM



All times are GMT -4. The time now is 07:43 PM.