getopt arguments

getopt arguments

Post by Todd » Thu, 22 Jun 2006 23:55:23



I have read the man pages but still don't understand getopt.

#!/bin/bash
#
#

SCRIPTNAME=${0##*/}

function show_list() {
         cat -n .dolist
         }

# commented out for debugging
#function addroutine() {
#       echo "$2 >> .dolist"
#       }

# commented out for debugging
#function delroutine() {
#       sed
#       }

function show_help() {
         echo "Usage: todo -[hrl:] "
         echo " 'something' (adds to da' list)"
         echo "          -h (Prints usage)"
         echo "          -r (removes line number specified)"
         echo "<blank>   -l (displays the todo list)"
exit 0
     }

#***********************************************************************
set -x

if [ $? -ne 0 ]
then
         show_help
         exit 1:
fi
set -- "$args"
DONE=false
while [ "$DONE" != "true" ]
     do
         case $1 in
          -h | --help)show_help            ;;
          "")show_list                           ;;
          --)DONE=true                     ;;

        esac
shift
done

-- INSERT --
                                         1,12          Top

 
 
 

getopt arguments

Post by Stephane Chazela » Fri, 23 Jun 2006 00:42:06



> I have read the man pages but still don't understand getopt.

[...]

Don't use getopt, it's deprecated and broken by design. Use
getopts instead.

--
Stephane

 
 
 

getopt arguments

Post by Chris F.A. Johnso » Fri, 23 Jun 2006 01:01:01



> I have read the man pages but still don't understand getopt.

> #!/bin/bash
> #
> #

> SCRIPTNAME=${0##*/}

> function show_list() {

    The portable way to declare a function (which works in all
    Bourne-type shells, including ksh) is:

show_list() {

    The ksh way (which also works in bash, but not other Bourne-type
    shells) is:

function show_list {

    What you have is a bash hybrid that will fail in all other shells.

>          cat -n .dolist
>          }

> # commented out for debugging
> #function addroutine() {
> #       echo "$2 >> .dolist"
> #       }

> # commented out for debugging
> #function delroutine() {
> #       sed
> #       }

> function show_help() {
>          echo "Usage: todo -[hrl:] "
>          echo " 'something' (adds to da' list)"
>          echo "          -h (Prints usage)"
>          echo "          -r (removes line number specified)"
>          echo "<blank>   -l (displays the todo list)"
> exit 0
>      }

> #***********************************************************************
> set -x


    Did you look at what $args contains after that command? That
    should be:


    or:


    (I'm assuming that your arguments to getopt are correct; I don't
    use it. GNU-style options are unnecessary and not portable; use
    the standard form, and getopts.)

Quote:> if [ $? -ne 0 ]
> then
>          show_help
>          exit 1:
> fi
> set -- "$args"

    If your call to getopt worked, this will only set a single
    argument: the entire contents of $args.

Quote:> DONE=false
> while [ "$DONE" != "true" ]
>      do
>          case $1 in
>           -h | --help)show_help       ;;
>           "")show_list                      ;;
>           --)DONE=true                ;;

>    esac
> shift
> done

while getopts rh opt
do
  case $opt in
       h) show_help ;;
      "") show_list ;;
  esac
done

   If you want to do anything after the options are parsed, follow
   that with:

shift $(( $OPTIND - 1 ))

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

getopt arguments

Post by Stephane Chazela » Fri, 23 Jun 2006 01:09:46


[...]

Quote:>     The ksh way (which also works in bash, but not other Bourne-type
>     shells) is:

It obviously works as well in zsh which is/has a superset of ksh88.
Quote:

> function show_list {

>     What you have is a bash hybrid that will fail in all other shells.

[...]

But contrary to ksh, bash and zsh don't interpret that syntax
differently than the standard one.

--
Stephane

 
 
 

getopt arguments

Post by Glenn Jackma » Fri, 23 Jun 2006 01:16:11



Quote:>  I have read the man pages but still don't understand getopt.

Use the builtin getopts.  Here's an example:

#!/bin/bash
show_usage() {
    echo usage:  foo [-h] [-x y] [-a b] mandatory_arg ...

Quote:}

x_arg="default X"
a_arg="default A"

while getopts ":hx:a:" option; do
    case "$option" in
        h) show_usage ; exit ;;
        x) x_arg="$OPTARG" ;;
        a) a_arg="$OPTARG" ;;
        ?) echo >&2 "illegal option '$OPTARG'"; exit 1 ;;
    esac
done
shift $(($OPTIND - 1)) ;# throw away the processed arguments

if [ $# -eq 0 ]; then
    echo >&2 "did not supply at least 1 mandatory arguments"
    exit 1
fi

echo "x: $x_arg"
echo "a: $a_arg"
echo "args: $*"

--
Glenn Jackman
Ulterior Designer

 
 
 

getopt arguments

Post by base6 » Fri, 23 Jun 2006 04:14:30




>> I have read the man pages but still don't understand getopt.

>> #!/bin/bash
>> #
>> #

>> SCRIPTNAME=${0##*/}

>> function show_list() {

>     The portable way to declare a function (which works in all
>     Bourne-type shells, including ksh) is:

> show_list() {

>     The ksh way (which also works in bash, but not other Bourne-type
>     shells) is:

> function show_list {

>     What you have is a bash hybrid that will fail in all other shells.

This works in ksh and ksh93

And, if memory serves, this is a ksh construct- not bash - and meant to
overcome the limitations of the POSIX variation.

 
 
 

getopt arguments

Post by base6 » Fri, 23 Jun 2006 04:11:42



> I have read the man pages but still don't understand getopt.

#!/bin/bash
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
yourSubroutine() {
     echo "$opt_t"
     }

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
usage() {
     echo "Usage: $ME -[ht:] (Version  $VERSION)"
     echo "    -h (Prints usage)"
     echo "    -t <does something>"
     exit 0
     }

#**********************************************************************
VERSION="1.0"
ME=${0##*/}                        # Short name of script
TMP=/tmp/${ME}.tmp
STDERR=/tmp/${ME}.stderr
STDOUT=/tmp/${ME}.stdout
while getopts "ht:" C
     do case ${C} in
         t)  opt_t=$OPTARG   ;;
         *)  usage           ;;
     esac done
yourSubroutine

> #!/bin/bash
> #
> #

> SCRIPTNAME=${0##*/}

> function show_list() {
>         cat -n .dolist
>         }

> # commented out for debugging
> #function addroutine() {
> #       echo "$2 >> .dolist"
> #       }

> # commented out for debugging
> #function delroutine() {
> #       sed
> #       }

> function show_help() {
>         echo "Usage: todo -[hrl:] "
>         echo " 'something' (adds to da' list)"
>         echo "          -h (Prints usage)"
>         echo "          -r (removes line number specified)"
>         echo "<blank>   -l (displays the todo list)"
> exit 0
>     }

> #***********************************************************************
> set -x

> if [ $? -ne 0 ]
> then
>         show_help
>         exit 1:
> fi
> set -- "$args"
> DONE=false
> while [ "$DONE" != "true" ]
>     do
>         case $1 in
>          -h | --help)show_help         ;;
>          "")show_list                   ;;
>          --)DONE=true                  ;;

>     esac
> shift
> done

> -- INSERT --                                         1,12          Top

 
 
 

getopt arguments

Post by Chris F.A. Johnso » Fri, 23 Jun 2006 04:39:15





>>> I have read the man pages but still don't understand getopt.

>>> #!/bin/bash
>>> #
>>> #

>>> SCRIPTNAME=${0##*/}

>>> function show_list() {

>>     The portable way to declare a function (which works in all
>>     Bourne-type shells, including ksh) is:

>> show_list() {

>>     The ksh way (which also works in bash, but not other Bourne-type
>>     shells) is:

>> function show_list {

>>     What you have is a bash hybrid that will fail in all other shells.

> This works in ksh and ksh93

   Which "this"?

   The three variations above are, the bash hybrid, the POSIX
   definition, and the ksh method (in fact, the original form; it
   preceded functions in the Bourne shell).

Quote:> And, if memory serves, this is a ksh construct- not bash - and meant to
> overcome the limitations of the POSIX variation.

   Rather, to overcome the limitations of ksh93's implementation of
   it.

   Both bash and the BSD shell (ash/dash on Linux) have a built-in
   command, local, which makes variable so declared local to the
   function; pdksh has local as a standard alias.

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

getopt arguments

Post by base6 » Fri, 23 Jun 2006 07:42:18






>>>> I have read the man pages but still don't understand getopt.

>>>> #!/bin/bash
>>>> #
>>>> #

>>>> SCRIPTNAME=${0##*/}

>>>> function show_list() {
>>>     The portable way to declare a function (which works in all
>>>     Bourne-type shells, including ksh) is:

>>> show_list() {

>>>     The ksh way (which also works in bash, but not other Bourne-type
>>>     shells) is:

>>> function show_list {

>>>     What you have is a bash hybrid that will fail in all other shells.
>> This works in ksh and ksh93

>    Which "this"?

The "this" immediately above that you imply "will fail in all other
shells"

Possibly, I've misunderstood your intent.

Quote:

>    The three variations above are, the bash hybrid, the POSIX
>    definition, and the ksh method (in fact, the original form; it
>    preceded functions in the Bourne shell).

>> And, if memory serves, this is a ksh construct- not bash - and meant to
>> overcome the limitations of the POSIX variation.

>    Rather, to overcome the limitations of ksh93's implementation of
>    it.

ksh93 is a *superset* of POSIX and, as such, it obviously meets
the standard.

Quote:

>    Both bash and the BSD shell (ash/dash on Linux) have a built-in
>    command, local, which makes variable so declared local to the
>    function; pdksh has local as a standard alias.

Yes, but "local" is only needed because of the poor design of the POSIX
function definition and it isn't part of POSIX, right?

Chris, I'm just jagging you a bit, it's not intended as vicious, so
don't take it that way :)

 
 
 

getopt arguments

Post by Chris F.A. Johnso » Fri, 23 Jun 2006 08:33:16







>>>>> I have read the man pages but still don't understand getopt.

>>>>> #!/bin/bash
>>>>> #
>>>>> #

>>>>> SCRIPTNAME=${0##*/}

>>>>> function show_list() {
>>>>     The portable way to declare a function (which works in all
>>>>     Bourne-type shells, including ksh) is:

>>>> show_list() {

>>>>     The ksh way (which also works in bash, but not other Bourne-type
>>>>     shells) is:

>>>> function show_list {

>>>>     What you have is a bash hybrid that will fail in all other shells.
>>> This works in ksh and ksh93

>>    Which "this"?

> The "this" immediately above that you imply "will fail in all other
> shells"

> Possibly, I've misunderstood your intent.

   Looks like it. "What you have" refers to "function show_list() {" .

- Show quoted text -

Quote:>>    The three variations above are, the bash hybrid, the POSIX
>>    definition, and the ksh method (in fact, the original form; it
>>    preceded functions in the Bourne shell).

>>> And, if memory serves, this is a ksh construct- not bash - and meant to
>>> overcome the limitations of the POSIX variation.

>>    Rather, to overcome the limitations of ksh93's implementation of
>>    it.

> ksh93 is a *superset* of POSIX and, as such, it obviously meets
> the standard.

>>    Both bash and the BSD shell (ash/dash on Linux) have a built-in
>>    command, local, which makes variable so declared local to the
>>    function; pdksh has local as a standard alias.

> Yes, but "local" is only needed because of the poor design of the POSIX
> function definition and it isn't part of POSIX, right?

   The alternative would be to make variables local unless exported
   (by whatever mechanism), which would break existing scripts. That
   was not the intent of the POSIX standard.

Quote:> Chris, I'm just jagging you a bit, it's not intended as vicious, so
> don't take it that way :)

   :)

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

1. spaces inside arguments while using getopt


| Then to make the rest of
| the command line arguments accessable use:
|  
|     shift `expr $OPTIND - 1`
|  
| and you can resume your script as normal with $1 $2 $3...

And indeed, that's what the man page for getopts says to do.

It always left me wondering why getopts was written that way, incrementing
$OPTIND ahead of itself and requiring the extra fork to expr.  If $OPTIND
were left pointing at the last option-related argument and not to the first
argument after the options, one could simply

    shift $OPTIND

but so much for fantasies; getopts, as written, forces the expr (unless
you're in a shell that includes arithmetic).

In protest, I use getopts this way:

        while getopts ...
        do ...
        done

        eval first_param=\${$OPTIND}
        shift $OPTIND
        second_param=$1 third_param=$2 ...

I guess this works too:


        shift $OPTIND
        first_param=$1 second_param=$2 third_param=$3 ...

2. Zip driver

3. getopt and arguments containing spaces

4. FTP Installation Problem

5. Using "getopt" to parse option arguments w/ spaces

6. New: HID driver causes slab corruption

7. argument parser strips out " in arguments

8. New to Linux, needs documentation

9. script questions - passing argument as argument to another program

10. newbie: (command and its arguments) as a sh script argument

11. Taking arguments from pipe 'plus' one more argument

12. pushd <no arguments> versus cd <no arguments>

13. The argument to end all pro-Linux arguments