Re: getopt and parameters with spaces
On Mon, Dec 17, 2007 at 06:29:01PM -0500, T o n g wrote:
> I seem to have found a bug of getopt from the util-linux
> package. Please correct me if I'm wrong.
>
> I believe that the suggested way to call getopt is
>
> eval set -- `getopt -o <options> -- "$@"`
No, it is actually:
eval set -- "`getopt -o <options> -- "$@"`"
Note the extra set of double quotes!
See also the example script (on debian installed in
/usr/share/doc/util-linux/examples/getopt-parse.bash.gz), which does it
in two steps, in order to preserve the return code:
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
That remark about the quotes is not there by accident :-)
> But I found that it can't handle parameters with spaces well:
>
> $ set -- -t 'test p 1' 2
[..]
> $ eval set -- `getopt -o t: -- "$@"`
>
> $ echo "'$2'"
> 'test p 1'
Correct. But with the extra double quotes around the backquotes, it
preserves the spaces correctly.
Have fun,
Frodo
--
Frodo Looijaard <frodo@frodo.looijaard.name> (See http://frodo.looijaard.name/)
Defenestration n. (formal or joc.):
The act of removing Windows from your computer in disgust, usually followed
by the installation of Linux or some other Unix-like operating system.
Reply to: