- 主题:mac有时候真的很神奇
getopt
getopts
只差一个字母,但两者有很大不同。
比如
/usr/bin/getopt 应该能处理长选项(long option),
shell内置getopts 只能处理短选项,
但是,mac 上的 getopt 和 getopts 差不多,处理不了长选项,
下面是 mac 上的 man getopt 的结果
GETOPT(1) BSD General Commands Manual GETOPT(1)
NAME
getopt -- parse command options
SYNOPSIS
args=`getopt optstring $*` ; errcode=$?; set -- $args
DESCRIPTION
The getopt utility is used to break up options in command lines for easy
parsing by shell procedures, and to check for legal options. Optstring
is a string of recognized option letters (see getopt(3)); if a letter is
followed by a colon, the option is expected to have an argument which may
or may not be separated from it by white space. The special option `--'
is used to delimit the end of the options. The getopt utility will place
`--' in the arguments at the end of the options, or recognize it if used
explicitly. The shell arguments ($1 $2 ...) are reset so that each
option is preceded by a `-' and in its own shell argument; each option
argument is also in its own shell argument.
EXAMPLES
The following code fragment shows how one might process the arguments for
a command that can take the options -a and -b, and the option -o, which
requires an argument.
args=`getopt abo: $*`
# you should not use `getopt abo: "$@"` since that would parse
# the arguments differently from what the set command below does.
if [ $? != 0 ]
then
echo 'Usage: ...'
exit 2
fi
set -- $args
# You cannot use the set command with a backquoted getopt directly,
# since the exit code from getopt would be shadowed by those of set,
# which is zero by definition.
for i
do
case "$i"
in
-a|-b)
echo flag $i set; sflags="${i#-}$sflags";
shift;;
-o)
echo oarg is "'"$2"'"; oarg="$2"; shift;
shift;;
--)
shift; break;;
esac
done
echo single-char flags: "'"$sflags"'"
echo oarg is "'"$oarg"'"
This code will accept any of the following as equivalent:
cmd -aoarg file file
cmd -a -o arg file file
cmd -oarg -a file file
cmd -a -oarg -- file file
SEE ALSO
sh(1), getopt(3)
DIAGNOSTICS
The getopt utility prints an error message on the standard error output
and exits with status > 0 when it encounters an option letter not
included in optstring.
HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Behavior
believed identical to the Bell version. Example changed in FreeBSD ver-
sion 3.2 and 4.0.
BUGS
Whatever getopt(3) has.
Arguments containing white space or embedded shell metacharacters gener-
ally will not survive intact; this looks easy to fix but isn't. People
trying to fix getopt or the example in this manpage should check the his-
tory of this file in FreeBSD.
The error message for an invalid option is identified as coming from
getopt rather than from the shell procedure containing the invocation of
getopt; this again is hard to fix.
The precise best way to use the set command to set the arguments without
disrupting the value(s) of shell options varies from one shell version to
another.
Each shellscript has to carry complex code to parse arguments halfway
correcty (like the example presented here). A better getopt-like tool
would move much of the complexity into the tool and keep the client shell
scripts simpler.
BSD April 3, 1999 BSD
--
修改:Bolyai FROM 14.112.219.*
FROM 14.112.219.*
getopts 是 shell 内置的命令,
而 getopt 是外置命令,位于 /usr/bin/getopt
https://man7.org/linux/man-pages/man1/getopts.1p.html
确实打破了我的想象。
【 在 Bolyai 的大作中提到: 】
: 比如 /usr/bin/getopt 并不能处理长选项,而是和 getopts 差不多
: GETOPT(1) BSD General Commands Manual GETOPT(1)
: NAME
: ...................
--
修改:Bolyai FROM 14.112.219.*
FROM 14.112.219.*
这从根源上要追溯到UNIX的不同流派。
Linux是各派的大杂烩(从sysv借鉴的比较多),而Mac os则起源于BSD
由于不同的UNIX流派有一些细微的不同,当个人在不同流派间切换使用的时候,会由于这些细微的差别而感到别扭。
这个从APUE中讲解不同UNIX版本的API也能感受出来。
【 在 Bolyai 的大作中提到: 】
: getopt
: getopts
: 只差一个字母,但两者有很大不同。
: ...................
--
FROM 111.196.68.*
所以,碰到异常了,要及时查根源,不要再瞎尝试了。
读英文毕竟没有中文那么方便,所以我折腾好久才想起要仔细读 manual,尬
【 在 seablue 的大作中提到: 】
: 这从根源上要追溯到UNIX的不同流派。
: Linux是各派的大杂烩(从sysv借鉴的比较多),而Mac os则起源于BSD
: 由于不同的UNIX流派有一些细微的不同,当个人在不同流派间切换使用的时候,会由于这些细微的差别而感到别扭。
: 这个从APUE中讲解不同UNIX版本的API也能感受出来。
--
FROM 183.30.219.*
写脚本 少用shell 用python,mac上有不少命令有些许区别。比如我比较喜欢的netstat -anp 在mac上不支持p
【 在 Bolyai (休止符) 的大作中提到: 】
: getopt
: getopts
: 只差一个字母,但两者有很大不同。
:
--
发自xsmth (iOS版)
--
※ 修改:·lcgogo 于 May 24 01:20:32 2022 修改本文·[FROM: 115.171.41.*]
※ 来源:·水木社区
http://m.mysmth.net·[FROM: 115.171.41.*]
修改:lcgogo FROM 115.171.41.*
FROM 115.171.41.*
我有时从Linux切到Mac上,除了最简单比如ls之类的,一般都不敢瞎打命令。
不看手册没有安全感。
因为不知道命令之间会不会有细微的差别,有的是命令拼法不同,有的是参数的作用不同。这些不同都是很细微的,很容易和已有的认知冲突。
我用mac一般不爱用命令行,只爱用它的GUI程序。要用命令行干活一般回到Linux上。
【 在 Bolyai 的大作中提到: 】
: 所以,碰到异常了,要及时查根源,不要再瞎尝试了。
: 读英文毕竟没有中文那么方便,所以我折腾好久才想起要仔细读 manual,尬
--
修改:seablue FROM 111.196.68.*
FROM 111.196.68.*
mac还不支持参数放末尾
比如 ls xxx -l会把-l当一个文件
【 在 Bolyai 的大作中提到: 】
: getopt
: getopts
: 只差一个字母,但两者有很大不同。
: ...................
--
FROM 115.171.189.*