getopts question

getopts question

Post by Jeffr » Fri, 09 Oct 1998 04:00:00



I am trying to write a script that can have at most 2 options and one
optional argument. So, the command line looks like:

ptree [-p] [-c] [PID]

I am trying to use getopts to parse out the options, and I need to set
the variable ID to [PID] is given, otherwise to $$. However, I can't get
the shifts worked out. I have read several sources, and I can't get it
worked out.

Code so far:
shift `expr $OPTIND - 1`
if [ $# != 1 -o $# != 0 ]
then
        echo message
fi

if [ $# == 1 ]
then $ID=$1
fi

if [ $# == 0 ]
then $ID=$$
fi

Any help would be appreciated.

Jeffrey West

 
 
 

getopts question

Post by Barry Margoli » Fri, 09 Oct 1998 04:00:00




>I am trying to write a script that can have at most 2 options and one
>optional argument. So, the command line looks like:

>ptree [-p] [-c] [PID]

>I am trying to use getopts to parse out the options, and I need to set
>the variable ID to [PID] is given, otherwise to $$. However, I can't get
>the shifts worked out. I have read several sources, and I can't get it
>worked out.

>Code so far:
>shift `expr $OPTIND - 1`
>if [ $# != 1 -o $# != 0 ]

-o should be -a.  Or you could use [ $# -gt 1 ].

Quote:>if [ $# == 1 ]
>then $ID=$1

That should be ID=$1, not $ID=$1.  I frequently make the same mistake.

Quote:>fi

>if [ $# == 0 ]
>then $ID=$$

That should be ID=$$, not $ID=$$.

Quote:>fi

A simpler way to specify the above defaulting is:

ID=${1-$$}

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

 
 
 

getopts question

Post by Niall Smar » Sat, 10 Oct 1998 04:00:00



> I am trying to write a script that can have at most 2 options and one
> optional argument. So, the command line looks like:

> ptree [-p] [-c] [PID]

> I am trying to use getopts to parse out the options, and I need to set
> the variable ID to [PID] is given, otherwise to $$. However, I can't get
> the shifts worked out. I have read several sources, and I can't get it
> worked out.

#!/bin/sh

usage () {
        echo "usage: $0 [ -p ] [ -c ] [ pid ]" >&2
        exit 1;

Quote:}

set -- `getopt pc $*`

[ $? -eq 0 ] || usage;

optp=0
optc=0

for i in $*; do
        case $i in
                -p)     optp=1;
                        shift;;
                -c)     optc=1;
                        shift;;
                --)     shift;
                        break;;
        esac
done

case $# in
        1)      pid=$1;;
        0)      pid=$$;;
        *)      usage;;
esac

echo $pid $optp $optc

Niall

 
 
 

getopts question

Post by Dan A. Merc » Sat, 10 Oct 1998 04:00:00


: I am trying to write a script that can have at most 2 options and one
: optional argument. So, the command line looks like:

: ptree [-p] [-c] [PID]

: I am trying to use getopts to parse out the options, and I need to set
: the variable ID to [PID] is given, otherwise to $$. However, I can't get
: the shifts worked out. I have read several sources, and I can't get it
: worked out.

What shell are you using?  It might help if you mentioned that before
asking any questions.  "[ $# == 1 ]" looks like ksh93 but
"$ID=$1" looks like perl.  I assume that was a mistake.

shift OPTIND-1   # OPTIND is an integer  so the math will be performed
                 # first. OPTIND - 1 also works.
ID="${1:-$$}"    # if $1 exists,  use its value as pid,  else use
                 # current script pid

: Code so far:
: shift `expr $OPTIND - 1`
: if [ $# != 1 -o $# != 0 ]
: then
:       echo message
: fi

: if [ $# == 1 ]
: then $ID=$1
: fi

: if [ $# == 0 ]
: then $ID=$$
: fi

: Any help would be appreciated.

: Jeffrey West

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

1. getopt question

I am trying to understand a C program that accepts command
line agreements (I have to write a few of this genre). It
uses a function called getopt (it has  an int, a pointer to
a pointer to char, and a string literal as it's parameters)
to process the command line argument. Which library contains
this function, I would like to look at the function
prototype. Also are there any web links on using this
function (or anything to do with it) available?

thanks in advance

2. Configure Modem and Sound????

3. getopts question

4. Exactly what directives will be affected by Limit,Files?

5. ksh getopts question

6. daemon improvements

7. getopts question

8. Newbie needs Ultra 5 install help

9. newbie: getopts question

10. Bourne shell programming (getopts) question

11. passing args to getopts when getopts is embedded in functions

12. getopt Vs getopts

13. getopts or getopt