请您提供有关 zsh 文档这一部分的见解。我在这个文档上花了很长时间,但机器人可以说清楚:http: //zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Alternate-Forms-For-Complex-Commands
我正在寻找所有命令的清晰示例if
for
foreach
while
until
repeat
case
select
function
。
这些只是形式更短一些命令,主要是摆脱“多余的”保留字一样then
,fi
,do
,done
,等长格式更便携; 短的只在zsh
.
例如长形式 if
if [[ -f file ]] ; then echo "file exists"; else echo "file does not exist"; fi
Run Code Online (Sandbox Code Playgroud)
不仅zsh
可以在其他 shell 中工作,也可以在其他 shell 中工作(用单个括号替换双括号以获得更多的便携性)
而短格式
if [[ -f file ]] { echo "file exists" } else { echo "file does not exist" }
if [[ -f file ]] echo file exists
Run Code Online (Sandbox Code Playgroud)
仅适用于zsh
.
另一个例子,这次是for
循环。
长格式:
for char in a b c; do echo $char; done
for (( x=0; x<3; x++ )) do echo $x; done
Run Code Online (Sandbox Code Playgroud)
短的:
for char in a b c; echo $char
for char (a b c) echo $char # second version of the same
foreach char (a b c); echo $char; end # csh-like 'for' loop
for (( x=0; x<3; x++ )) echo $x # c++ version
for (( x=0; x<3; x++ )) { echo "$x"; } # also works in bash and ksh
Run Code Online (Sandbox Code Playgroud)
我相信你明白了——我们只是删除了不必要的词,如果列表需要与其他东西分开,用{}
. 其余命令:
尽管
x=0; while ((x<3)); do echo $((x++)); done # long
x=0; while ((x<3)) { echo $((x++)) } # short
x=0; while ((x<3)) echo $((x++)) # shorter for single command
Run Code Online (Sandbox Code Playgroud)直到
x=0; until ((x>3)); do echo $((x++)); done # long
x=0; until ((x>3)) { echo $((x++)) } # short
x=0; until ((x>3)) echo $((x++)) # shorter for single command
Run Code Online (Sandbox Code Playgroud)重复
repeat 3; do echo abc; done # long
repeat 3 echo abc # short
Run Code Online (Sandbox Code Playgroud)案件
word=xyz; case $word in abc) echo v1;; xyz) echo v2;; esac # long
word=xyz; case $word { abc) echo v1;; xyz) echo v2 } # short
Run Code Online (Sandbox Code Playgroud)选择
select var in a b c; do echo $var; done # long
select var in a b c; echo $var # short
select var (a b c) echo $var # shorter
Run Code Online (Sandbox Code Playgroud)功能
function myfun1 { echo abc; } # long
function myfun2; echo abc # short
myfun3() echo abc # shorter and Bourne-compatible
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
758 次 |
最近记录: |