getopt.1 (18b3ba267cff8d71364cbabb5429eccf1e79bba4) | getopt.1 (0ab2a7ae85e2d50dcafed917b86006b751783ca3) |
---|---|
1.Dd April 3, 1999 2.Dt GETOPT 1 3.Os 4.Sh NAME 5.Nm getopt 6.Nd parse command options 7.Sh SYNOPSIS | 1.Dd April 3, 1999 2.Dt GETOPT 1 3.Os 4.Sh NAME 5.Nm getopt 6.Nd parse command options 7.Sh SYNOPSIS |
8.Ic set \-\- \`getopt Ar optstring | 8.Ic var=\`getopt Ar optstring |
9.Qq $@ | 9.Qq $@ |
10\` | 10\` ; set \-\- $var |
11.Sh DESCRIPTION 12.Nm Getopt 13is used to break up options in command lines for easy parsing by 14shell procedures, and to check for legal options. 15.Ar Optstring 16is a string of recognized option letters (see 17.Xr getopt 3 18); --- 26 unchanged lines hidden (view full) --- 45.Pp 46.Bd -literal -offset indent 47tmp=$(getopt abo: "$@") 48if [ $? != 0 ] 49then 50 echo 'Usage: ...' 51 exit 2 52fi | 11.Sh DESCRIPTION 12.Nm Getopt 13is used to break up options in command lines for easy parsing by 14shell procedures, and to check for legal options. 15.Ar Optstring 16is a string of recognized option letters (see 17.Xr getopt 3 18); --- 26 unchanged lines hidden (view full) --- 45.Pp 46.Bd -literal -offset indent 47tmp=$(getopt abo: "$@") 48if [ $? != 0 ] 49then 50 echo 'Usage: ...' 51 exit 2 52fi |
53eval set \-\- $tmp | 53set \-\- $tmp |
54for i 55do 56 case "$i" 57 in 58 \-a|\-b) | 54for i 55do 56 case "$i" 57 in 58 \-a|\-b) |
59 echo flag $i set; sflags="${i#-}$sflags"; shift;; | 59 echo flag $i set; sflags="${i#-}$sflags"; 60 shift;; |
60 \-o) | 61 \-o) |
61 echo oarg is "'"$2"'"; oarg="$2"; shift; shift;; | 62 echo oarg is "'"$2"'"; oarg="$2"; shift; 63 shift;; |
62 \-\-) 63 shift; break;; 64 esac 65done 66echo single-char flags: $sflags 67echo oarg is "'"$oarg"'" 68.Ed 69.Pp 70This code will accept any of the following as equivalent: 71.Pp 72.Bd -literal -offset indent 73cmd \-aoarg file file 74cmd \-a \-o arg file file 75cmd \-oarg -a file file 76cmd \-a \-oarg \-\- file file 77.Pp | 64 \-\-) 65 shift; break;; 66 esac 67done 68echo single-char flags: $sflags 69echo oarg is "'"$oarg"'" 70.Ed 71.Pp 72This code will accept any of the following as equivalent: 73.Pp 74.Bd -literal -offset indent 75cmd \-aoarg file file 76cmd \-a \-o arg file file 77cmd \-oarg -a file file 78cmd \-a \-oarg \-\- file file 79.Pp |
78Test your scripts with calls like this 79.Pp 80cmd \-ab \-o 'f \-z' 81.Pp 82to verify that they work with parameters/filenames that have 83whitespace in them. | |
84.Ed 85.Sh SEE ALSO 86.Xr sh 1 , 87.Xr getopt 3 88.Sh DIAGNOSTICS 89.Nm Getopt 90prints an error message on the standard error output and exits with 91status > 0 when it encounters an option letter not included in 92.Ar optstring . 93.Sh HISTORY 94Written by Henry Spencer, working from a Bell Labs manual page. 95Behavior believed identical to the Bell version. Example replaced in 96.Fx 97version 3.2 and 4.0. 98.Sh BUGS 99Whatever 100.Xr getopt 3 101has. 102.Pp | 80.Ed 81.Sh SEE ALSO 82.Xr sh 1 , 83.Xr getopt 3 84.Sh DIAGNOSTICS 85.Nm Getopt 86prints an error message on the standard error output and exits with 87status > 0 when it encounters an option letter not included in 88.Ar optstring . 89.Sh HISTORY 90Written by Henry Spencer, working from a Bell Labs manual page. 91Behavior believed identical to the Bell version. Example replaced in 92.Fx 93version 3.2 and 4.0. 94.Sh BUGS 95Whatever 96.Xr getopt 3 97has. 98.Pp |
103It is hard to get command switch parsing right in shell scripts, 104especially with arguments containing whitespace or embedded shell 105metacharacters. This version of 106.Nm getopt 107and the example in this manpage have been fixed to avoid traditional 108problems. They have been tested with 109.Fx 110.Xr sh 1 111and with GNU bash. Note that bash has a builtin 112.Nm getopt . 113In shells with builtin 114.Nm getopt 115you need to call getopt with a full path to get the external version 116described here. | 99Arguments containing white space or embedded shell metacharacters 100generally will not survive intact; this looks easy to fix but isn't. |
117.Pp 118The error message for an invalid option is identified as coming 119from 120.Nm getopt 121rather than from the shell procedure containing the invocation 122of 123.Nm getopt ; | 101.Pp 102The error message for an invalid option is identified as coming 103from 104.Nm getopt 105rather than from the shell procedure containing the invocation 106of 107.Nm getopt ; |
124this is hard to fix. | 108this again is hard to fix. 109.Pp 110The precise best way to use the 111.Nm set 112command to set the arguments without disrupting the value(s) of 113shell options varies from one shell version to another. |