circulation
Table of Contents

循环

if

if commands; then
    commands
[elif commands; then
    commands...]
[else
    commands]
fi
FILE=~/.zshrc # 随便找个路径
if [ -e "$FILE" ]; then # -e 单目操作符
    if [ -f "$FILE" ]; then
        echo "$FILE is a regular file."
    fi
    if [ -d "$FILE" ]; then
        echo "$FILE is a directory."
    fi
    if [ -r "$FILE" ]; then
        echo "$FILE is readable."
    fi
    if [ -w "$FILE" ]; then
        echo "$FILE is writable."
    fi
    if [ -x "$FILE" ]; then
        echo "$FILE is executable/searchable."
    fi
else
    echo "$FILE does not exist"
fi

case

case 其实就是我们熟悉的那个 swich ,但语法形式上有很大的不同。

case "$variable" in
    "$condition1" )
        command...
    ;;
    "$condition2" )
        command...
    ;;
esac

来个例子。

x=4

case $x in
    'a' )
        echo "x 是 a";;
    4 )
        echo "x 是 4";;
    'b' )
        echo "x 是 b"
esac

# x 是 4

for

for variable [in words]; do
    commands
done
for i in *
do
    echo $i;
done

## 会打印当前目录下的所有文件名
for i in {1..5}
do 
echo $i
done

>>>
1
2
3
4
5
for FILE in $HOME/.bash*
do 
echo $FILE
done

>>>
/Users/xhxu/.bash_history
/Users/xhxu/.bash_profile
/Users/xhxu/.bash_sessions

while

count=1
while [ $count -le 5 ]; do
    echo $count
    count=$((count + 1))
done
echo "Finished."

# 依次打印 1 - 5 和 finished

语法如下:

while commands; do commands; done

read 读取

读取每一行内容

$ cat 1 | while read line ; do echo $line ; done
1
2
3
4
5

break

跳出所有循环

break n

跳出第n层循环

continue

跳出当前循环