我可以将管道运算符用作 OR 语句吗?
Can I use the pipe operator as an OR statement?
我正在阅读一本关于 shell 命令的介绍性书籍,它建议使用管道命令作为 OR 语句,如下所示:
case "" in
start|START)
/usr/bin/sshd
;;
stop|STOP)
kill $(cat/var/run/sshd.pid)
;;
esac
为什么这work/what是背后的逻辑?
正如 phoxis 所评论的那样,虽然它与用于管道的字符相同,但在这种情况下,它是 case
语句的特殊语法的一部分,用于定义多个将导致执行的值以下语句(直到 ;;
分隔符)。
这里不是管道操作员。它在 case
语法中。阅读 man bash
并转到 "Compound Commands" 部分并阅读案例。
这是 bash 手册的摘录
case <i>word</i> in [ [(] <i>pattern</i> [ | <i>pattern</i> ] ... ) <i>list</i> ;; ] ... esac
A case
command first
expands word
, and tries to match it against each pattern
in
turn, using the same matching rules as for pathname expansion (see
Pathname Expansion below). The word is expanded using tilde
expansion, parameter and variable expansion, arithmetic
substitution, command substitution, process substitution and quote
removal. Each pattern
examined is expanded using tilde expansion,
parameter and variable expansion, arithmetic substitution, command
substitution, and process substitution. If the shell option
nocasematch
is enabled, the match is performed without regard to the
case of alphabetic characters. When a match is found, the
corresponding list
is executed. If the ;;
operator is used, no
subsequent matches are attempted after the first pattern match. Using
;&
in place of ;;
causes execution to continue with the list
associated with the next set of patterns. Using ;;&
in place of
;;
causes the shell to test the next pattern list in the statement,
if any, and execute any associated list
on a successful match.
The exit status is zero if no pattern matches. Otherwise, it is the
exit status of the last command executed in list
.
这是语法
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
这是
case "" in
start|START)
匹配上面的语法,它告诉 at-least 一个模式,或者多个由 |
,一个竖线分隔的模式。
我正在阅读一本关于 shell 命令的介绍性书籍,它建议使用管道命令作为 OR 语句,如下所示:
case "" in
start|START)
/usr/bin/sshd
;;
stop|STOP)
kill $(cat/var/run/sshd.pid)
;;
esac
为什么这work/what是背后的逻辑?
正如 phoxis 所评论的那样,虽然它与用于管道的字符相同,但在这种情况下,它是 case
语句的特殊语法的一部分,用于定义多个将导致执行的值以下语句(直到 ;;
分隔符)。
这里不是管道操作员。它在 case
语法中。阅读 man bash
并转到 "Compound Commands" 部分并阅读案例。
这是 bash 手册的摘录
case <i>word</i> in [ [(] <i>pattern</i> [ | <i>pattern</i> ] ... ) <i>list</i> ;; ] ... esac
A
case
command first expandsword
, and tries to match it against eachpattern
in turn, using the same matching rules as for pathname expansion (see Pathname Expansion below). The word is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, process substitution and quote removal. Eachpattern
examined is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell optionnocasematch
is enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the correspondinglist
is executed. If the;;
operator is used, no subsequent matches are attempted after the first pattern match. Using;&
in place of;;
causes execution to continue with thelist
associated with the next set of patterns. Using;;&
in place of;;
causes the shell to test the next pattern list in the statement, if any, and execute any associatedlist
on a successful match. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed inlist
.
这是语法
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
这是
case "" in start|START)
匹配上面的语法,它告诉 at-least 一个模式,或者多个由 |
,一个竖线分隔的模式。