Zshの複雑なコマンドの代替形式の例

Zshの複雑なコマンドの代替形式の例

洞察力を提供できますか?複雑なコマンドの代替形式

この文書の作成に長い時間を費やしましたが、より明確にすることができます。

if、、、、、、、、forの明確な例foreachを探してwhileいます。untilrepeatcaseselectfunction

ベストアンサー1

これはいくつかのコマンドの短い形式に過ぎず、主に、、thenなどfiの「冗長」予約語をdo削除します。done長い形式は移植性が良く、短い形式はzsh


たとえば、長い形式if

if [[ -f file ]] ; then echo "file exists"; else echo "file does not exist"; fi

他のシェルでのみ機能するのではなく、zsh他のシェルでも機能します(より移植性のために、二重角かっこを単一括弧で置き換えます)。

短い形式

if [[ -f file ]] { echo "file exists" } else { echo "file does not exist" }
if [[ -f file ]] echo file exists

にのみ適用されますzsh


別の例は今回はforループです。

長いフォーマット:

for char in a b c; do echo $char; done
for (( x=0; x<3; x++ )) do echo $x; done

短い:

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

皆さんもご理解いただけると確信しています。不要な単語を削除してリストを他のコンテンツから分離する必要がある場合は、角かっこを使用してください{}。残りのコマンド:

  • しかし、

    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
    
  • ~まで

    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
    
  • 繰り返す

    repeat 3; do echo abc; done                   # long
    repeat 3 echo abc                             # short
    
  • ケース

    word=xyz; case $word in abc) echo v1;; xyz) echo v2;; esac   # long
    word=xyz; case $word { abc) echo v1;; xyz) echo v2 }         # short
    
  • 選ぶ

    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
    
  • 機能

    function myfun1 { echo abc; }            # long
    function myfun2; echo abc                # short
    myfun3() echo abc                        # shorter and Bourne-compatible
    

おすすめ記事