[Bug 215207] [patch] getopt(1) example mangles command-line arguments with embedded spaces

bugzilla-noreply at freebsd.org bugzilla-noreply at freebsd.org
Sun Dec 11 08:35:09 UTC 2016


https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=215207

            Bug ID: 215207
           Summary: [patch] getopt(1) example mangles command-line
                    arguments with embedded spaces
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Keywords: patch
          Severity: Affects Many People
          Priority: ---
         Component: bin
          Assignee: freebsd-bugs at FreeBSD.org
          Reporter: dds at FreeBSD.org
          Keywords: patch

Created attachment 177862
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=177862&action=edit
Proposed fix

The provided getopt(1) example in the manual page mangles command-line
arguments containing spaces by splitting them through IFS.

Consider the following minimal example script, written by following the man
page example.

#!/bin/sh
args=`getopt a: $*`
if [ $? -ne 0 ]; then
   echo 'Error'
   exit 2
fi

set -- $args
while :; do
   case "$1" in
   -a)
           flags=-a
           shift
           ;;
   --)
           shift; break
           ;;
   esac
done

for i; do
    echo "Arg $i"
    shift
done

When the above script is run with 'a b' as an argument, it will output
Arg a
Arg b
rather than
Arg a b

The problem can be fixed by applying the attached patch.  Note that the patch
does not address option arguments with embedded spaces.

-- 
You are receiving this mail because:
You are the assignee for the bug.


More information about the freebsd-bugs mailing list