Linux Command Lists
Linux command lists —— ;, &&, ||.
continuous#
使用空格或分号(;)可执行无相关性的连续命令:
faner@FAN-MB0:~|⇒ test1='test 1' test2='test 2'
faner@FAN-MB0:~|⇒ echo $test1
test 1
faner@FAN-MB0:~|⇒ echo $test2
test 2
faner@FAN-MB0:~|⇒ echo $test1;echo $test2
test 1
test 2
faner@MBP-FAN:~|⇒ testShellVar=string; cd ~/Downloads
faner@MBP-FAN:~/Downloads|⇒ echo $testShellVar
string
faner@MBP-FAN:~/Downloads|⇒
Commands separated by a
;are executed sequentially
AND/OR#
&& 和 || 则可连续执行相关性的命令。
AND and OR lists are sequences of one or more pipelines separated by the && and || control operators, respectively. AND and OR lists are executed with left associativity.
command1 || command2:在逻辑上只要有第一条命令执行成功就不会执行第二条命令,只有第一条命令执行失败才会启动执行第二条命令。
command2 is executed if and only if command1 returns a non-zero exit status.
command1 && command2:只有在第一条命令执行成功时才会启动执行第二条命令。
command2 is executed if, and only if, command1 returns an exit status of zero.
mkdir homebrew 正确执行完毕,即成功创建目录 homebrew,才会启动执行后面的 curl -L 命令。
这些符号为 BASH 的 token(control operator)。
demo#
- 下面的 bash 系统级配置
/etc/profile的内容:
$ cat /etc/profile
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
# 当 `/etc/bashrc` 文件存在且可读时,source 引入。
[ -r /etc/bashrc ] && . /etc/bashrc
fi
- vim-interaction.plugin.zsh 中判等串联执行命令:
# If before or after commands begin with : and don't end with <cr>, append it
[[ ${after} = :* && ${after} != *\<cr\> ]] && after+="<cr>"
[[ ${before} = :* && ${before} != *\<cr\> ]] && before+="<cr>"
# Open files passed (:A means abs path resolving symlinks, :q means quoting special chars)
[[ $# -gt 0 ]] && files=':args! '"${@:A:q}<cr>"
-
发送 2 次 ping 请求,每次最长等待 500ms,只要收到一个响应即退出。
- 2*500ms 内收到响应,命令成功返回(
$?为 0),执行 && 后面的命令,输出 “Google: ping OK”。 - 否则,命令执行失败(
$?为 1 或其他非零值),则短路执行 || 后面的命令,输出 “Google: ping fail”。
- 2*500ms 内收到响应,命令成功返回(
ping -o -c 2 -W 500 www.google.com > /dev/null 2>&1\
&& echo "Google: ping OK"\
|| echo "Google: ping fail"
- 下面是 RapaNui - getopts-12523979.sh 中判断串联、并联执行命令:
-
当前目录下如果有
forms-debug文件夹则进入,否则先创建再进入。