From: Todd on
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
args='getopt rh: -l help -- "$@"'
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
From: Stephane Chazelas on
On Wed, 21 Jun 2006 14:55:23 GMT, Todd wrote:
> 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
From: Chris F.A. Johnson on
On 2006-06-21, Todd wrote:
> 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
> args='getopt rh: -l help -- "$@"'

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

args=`getopt rh: -l help -- "$@"`

or:

args=$( getopt rh: -l help -- "$@" )

(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.)

> 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.

> 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
From: Stephane Chazelas on
On Wed, 21 Jun 2006 12:01:01 -0400, Chris F.A. Johnson wrote:
[...]
> 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.
>
> 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
From: Glenn Jackman on
At 2006-06-21 10:55AM, Todd <Todd(a)STOPSPAMacpdata.com> wrote:
> 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 ...
}

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
 |  Next  |  Last
Pages: 1 2
Prev: list of Dirs
Next: xargs rm -rf in crontab