如何在shell脚本中对简写运算符进行分组?
此 shell 脚本采用一个 -P 可选选项。
#!/bin/sh
usage() {
echo "Usage: $0 [-P path] URL [URL...]";
exit 0;
}
P='/default_path/'
getopts "P:" OPT && [ "$OPT" == 'P' ] && P=$OPTARG || usage
如果有选项,请执行以下操作。
如果选项是“P”,则将 $OPTARG 路径分配给变量 P。
如果选项不是“P”,则打印用法并退出。
当没有选项时,上面的语句定义失败。当没有给出选项时,getopts "P:" OPT
返回 false,然后调用 usage
。根据想法,我应该对表达式的后面部分进行分组,如下所示。但它给了我语法错误。
getopts "P:" OPT && { [ "$OPT" == 'P' ] && P=$OPTARG || usage }
如何在shell脚本中对简写运算符进行分组?
This shell script takes one -P optional option.
#!/bin/sh
usage() {
echo "Usage: $0 [-P path] URL [URL...]";
exit 0;
}
P='/default_path/'
getopts "P:" OPT && [ "$OPT" == 'P' ] && P=$OPTARG || usage
If there are options, do the followings.
If the option is 'P', then assign the $OPTARG path to the variable P.
If the option is not 'P', print the usage and exit.
The statement above fails the definition when there is no option. When no option is given, getopts "P:" OPT
returns false, then usage
is called. By idea, I should group the later parts of the expression, as below. But it gives me syntax error.
getopts "P:" OPT && { [ "$OPT" == 'P' ] && P=$OPTARG || usage }
How to group shorthand operators in shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短答案:使用后添加半颜色。
旁注:实际上,我选择使用getopt而不是getopts。它更具用途广泛,而且绝对值得学习它。。
旁注2:SE中有数百个线程谈论Shell脚本,其中一些线程具有数千个upvotes。但是知识是如此分散,而有些人可能专注于用法,缺乏真实的解释。请注意,
man bash
首先的资源(我不知道我实际上可以man bash
,因为我认为bash
不是命令)。 man bash`] [2]:
simple命令:
命令是也是管道:
第一个&&表格a list :
尽管第一个
&&
表现得像逻辑,但我尝试以这种方式看到 。让我们闯入第二部分。这是 test 表达式,即 compound命令:
同样,这是通过
&&
和||
加入的三个管道,上面解释了:如果我们需要对这些管道进行分组,则需要使用以下格式。它
必须用newline或Semicolon终止。
最后加入第一个
简单命令
&&复合命令
:无法想到此标头名称...
使用圆括号在语法上是正确的。但是它无法设置变量
p
。这是因为分配将在子壳中运行。Short answer: Add a semi-colon after usage.
Side note: Actually I opted to use getopt instead of getopts. It is much more versatile and definitely worth learning it.
Side note 2: There are hundreds of threads in SE talking about shell scripts, some with thousands of upvotes. But knowledge is so scattered, while some may focus on usage and lack authentic explanations. Be reminded that
man bash
is a very cool resource to begin with (which I did not know I can actuallyman bash
as I thoughtbash
was not a command).Explanation, with reference to [`man bash`][2]:
Simple Command:
A command is also a Pipeline:
The first && forms a List:
Although the first
&&
behaves like a logical AND, I try to not see it in that way.Let us break to the second part. This is a test expression, which is a Compound Command:
Again, these are three pipelines joining by
&&
and||
, which explained above:If we need to group these pipelines, we need to use the following format. It
must be terminated with a newline or semicolon.
Finally joining the first
Simple Command
&&Compound Commands
:Cannot think of this header name ...
Using round brackets is syntactically correct. But it cannot set the the variable
P
. It is because the assignment will be running in a subshell.