It is currently Thu, 27 Nov 2014 21:25:29 GMT



 
Author Message
 parse command line argument

Hi,

Can someone provide the soultion for the following:

I need a script which can process command line argument with

checker -Usa -Pxxx -Syyy
or
checker -U sa -P xxx -S yyy
or
checker -Usa -P xxx -S yyy
or
checker -Usa -Pxxx -Syyy
basic combinations of all with 0 or more spaces between -{UPS} and actual value.
-U needs to be sa, otherwise generating error message.

thanks,

Paul.



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument
| I need a script which can process command line argument with
|
| checker -Usa -Pxxx -Syyy
| or
| checker -U sa -P xxx -S yyy
| or
| checker -Usa -P xxx -S yyy
| or
| checker -Usa -Pxxx -Syyy
| basic combinations of all with 0 or more spaces between -{UPS} and actual valu
| -U needs to be sa, otherwise generating error message.

The Bourne shell doesn't have the string processing facilities built
in to break down strings, like extracting xxx from Pxxx, so we rely
on external commands like sed to do this.  In this case, I'd make
sed do that in one pass over the entire argument list, so I don't have
to account for all these A B vs. AB variations in the shell logic.

   set dummy `echo $* | sed 's/\(-[UPS]\)/\1 /g'`
   shift

"sed" inserts a space after -U, -P and -S, and "set ``" installs the
result as the new argument list.  "dummy" and "shift" prevent weird
behavior in the case where there are no arguments.

From here it's simple enough to rake in the arguments in a "while"
loop, using "shift" to advance to the next pair:

   while :
   do
      case $# in 0) break;; esac
      case $1 in
      -U)  ...;; -P)  ...;; -S)  ...;;
      *)   ... error ;;
      esac
      shift; shift
   done

        Donn Cave, University Computing Services, University of Washington
        d...@u.washington.edu



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument

{ snip ] Err.... Whatever happened to the "getopts" command -- AFAIK
it's available in every Bourne shell out there. "man getopts" for info
on using this parser.



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument

I used something like this:

The -o option works for "-ooption" "-o option"

#!/bin/sh

usage() {
        echo `basename $0`: ERROR: $* 1>&2
        echo usage: `basename $0` '[-[abc]] [-o file]' '[file ...]' 1>&2
        exit 1

inside() {
# this function returns a TRUE if $2 is inside $1
# I'll use a case statement, because this is a built-in of the shell,
# and faster.
# I could use grep:
#    echo $1 | grep -s "$2" >/dev/null
# or expr:
#   expr "$1" : ".*$2" >/dev/null && return 0 # true
# but case does not require another shell
    case "$1" in
        *$2*) return 0;;
    esac
    return 1;

done_options=
more_options() {
        # return true(0) if there are options left to parse
        # otherwise, return false

        # check the 'short-circuit' flag
        test $done_options && return 1  # true

        # how many arguments are left?
        [ $# -eq 0 ] && return 0

        # does the next argument start with a hyphen
        inside "$1" '-' && return 0;

        # otherwise, return false
        return 1        # false
a= b= c= o=

while more_options "$1"
do
    case "$1" in
        --) done_options=1;;
        -[abc]*)
                inside "$1" a && a=1;
                inside "$1" b && b=1;
                inside "$1" c && c=1;
                inside "$1" "[d-zA-Z]" &&
                        usage "unknown option in $1";
                ;;
        -o?*)
                # have to extract string from argument
                o=`expr "$1" : '-o\e(.*\e)'`
                ;;
        -o)
                [ $# -gt 1 ] || usage "-o requires a value";
                shift
                o="$1";;
        -*) usage "unknown option $1";;
    esac
    shift       # each time around, pop off the option
done
# continue with script
# ...

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument

Oops! - it was formated for nroff.
change \e to \
i.e.

Sorry, gang...

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument
Why wouldn't you use getopt (or getopts, I forget which is the new one)?
It's portable AFAIK and handles all permutations of options and arguments.

--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument
Bruce,
This is nifty, buy why wouldn't you just use getopts(1)?  :-)

 <lengthy shell script snipped>

--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.



 Sat, 23 Dec 2000 03:00:00 GMT   
 parse command line argument

      Hello,

here is a simple sample script using getopts
If no argument is specified, nothing is done, so you need to test this condition
yourself :-)
Use man getopts to have some details, and man ksh to have more details.

#!/bin/ksh
while getopts ":U:P:S:" Opt
do
  case $Opt in
    U )
        # option U specified, getting argument for the option
        Option_U_Argument=$OPTARG
        # not the same test as other one because argument is mandatory
        # BUT Why testing the presence of a mandatory argument, just set it !!!
        #
        if [ "$Option_U_Argument" = "sa" ]
        then
          # OK
          echo "Option -U has $Option_U_Argument as argument"
        else
                  echo "argument missing or invalid for -U argument, must be sa\n"
        fi
        ;;

    P )
        # option P specified, getting argument for the option
        Option_P_Argument=$OPTARG
        case $Option_P_Argument in
          xxxx    )
                  # What you need to do with the P xxxx argument
                  ;;
              *   )
                  echo " Invalid argument specified for the  -P argument:\n"
                  exit 1
                  ;;
        esac
        ;;

    S )
        # option S specified, getting argument for the option
        Option_S_Argument=$OPTARG
        case $Option_S_Argument in
          yyyy    )
                  # What you need to do with the S yyyy argument
                  ;;
              *   )
                  echo " Invalid argument specified for the -S argument:\n"
                  exit 1
                  ;;
        esac
        ;;

    "?" )
        # option is invalid
        echo "Invalid option specified $OPTARG"
        exit 1
        ;;

    ":" )
        # missing argument to an option
        echo "Option -$OPTARG need an argument"
        exit 1
        ;;
  esac
done

#
# Rest of the script, using the differents arguments given on the
#  command line



 Sun, 24 Dec 2000 03:00:00 GMT   
 parse command line argument
"Kurt J. Lanza" <k...@inforonics.com> writes:
| { snip ] Err.... Whatever happened to the "getopts" command -- AFAIK
| it's available in every Bourne shell out there. "man getopts" for info
| on using this parser.

You wouldn't believe how many people around here are still using Ultrix -
those old MIPS DECstations stubbornly refuse to die, and while I myself
would probably run NetBSD on them, they usually run some Ultrix legacy.

$ sh -c 'getopts'
sh: getopts: not found
$ man getopts
No manual entry for getopts.

        Donn Cave, University Computing Services, University of Washington
        d...@u.washington.edu



 Sun, 24 Dec 2000 03:00:00 GMT   
 parse command line argument
What about getopt (singular)?  IIRC getopt is the older method and getopts is
the newer posix.2 version.  You may have the older version.

--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.



 Mon, 25 Dec 2000 03:00:00 GMT   
 
   [ 10 post ] 

Similar Threads

1. parsing command line arguments

2. getopts and command line parsing

3. parsing command line: array problems

4. How to parse command lines

5. Command line parsing utility? - not getopt(s)

6. Parsing The Command Line

7. Command line parsing question

8. Parsing command-line, library?

9. Help parsing command line with XrmParseCommand()

10. Parsing command line options in C shell


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software