Im reading an introductory book on shell commands and it suggests using a pipe command as an OR statement like below:
case "$1" in
start|START)
/usr/bin/sshd
;;
stop|STOP)
kill $(cat/var/run/sshd.pid)
;;
esac
Why does this work/what is the logic behind it?
Here it is not the pipe operator. It is within the case
syntax. Read man bash
and go to the "Compound Commands" sections and read about the case.
Here is the extract of what the bash manual has to say
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... 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
.
This is the syntax
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
Which is
case "$1" in start|START)
matches the above syntax which tells at-least one pattern, or more than one separated by a |
, a vertical bar.