shell流程控制

if判断

基本语法

# 单分支
if 判断条件;then
    执行命令
fi

# 双分支
if 判断条件;then
    执行命令
else
    执行命令
fi

# 多分支
if 判断条件;then
    执行命令
elif 判断条件;then
    执行命令
else
    执行命令
fi

代码示例

# 单分支
[root@head test]# cat test.sh 
#!/usr/bin/bash

answer=$((RANDOM %10)) # 随机生成一个10以内的数字
read -p "please enter your number: " number

if [ $number -eq $answer ];then
    echo "you are right"
fi

# 双分支
[root@head test]# cat test.sh 
#!/usr/bin/bash

answer=$((RANDOM %10))
read -p "please enter your number: " number

if [ $number -eq $answer ];then
    echo "you are right"
else
    echo "you are wrong"
fi

# 多分支
#!/usr/bin/bash
  
answer=$((RANDOM %10))
while true
do
        read -p "please enter your number: " number
        if [ "$number" -gt 0 ] 2>/dev/null;then
                if [ $number -eq $answer ];then
                        echo "you are right"
                        exit
                elif [ $number -lt $answer ];then
                        echo "your number is too small"
                else
                        echo "you number is too big"
                fi
        else
                exit
        fi
done

 case判断

基本语法

case 变量 in
模式1)
    命令1
    ;;
模式2)
    命令2
    ;;
模式3)
    命令3
    ;;
*)
    无匹配后执行的命令
esac

代码示例

# 示例一:
[root@head test]# cat test.sh
#!/usr/bin/bash

ARGS=`getopt -o a:b:c: --long aaa:,bbb:,ccc: -n "$0" -- "$@"`
if [ $? -ne 0 ];then
    echo "params error" 
    exit 1
fi
eval set -- "${ARGS}" # 收集参数

while true
do
    case "$1" in 
        -a|--aaa)
            echo "first param: $2"
            aaa=$2
            shift 2 # 作用相当于将上面收集的参数的前两个丢掉,所以下面的参数还是获取的$2
            ;;
        -b|--bbb)
            echo "second param: $2"
            bbb=$2
            shift 2
            ;;
        -c|--ccc)
            echo "third param: $2"
            ccc=$2
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error"
            exit 1
    esac
done

echo $aaa
echo $bbb
echo $ccc


# 示例二:
[root@head test]# cat test.sh 
#!/usr/bin/bash

case $1 in
    "water"|"rice")
        echo "you are health"
        ;;
    "apple"|"milk")
        echo "you are good"
        ;;
    *)
        echo "you are not good"
        ;;
esac

while和until循环

基本语法

# while 条件为真时一直循环,条件为假时循环结束
while 条件
do
    循环体
done

# until 直到条件为真时才停止循环
until 条件
do
    循环体
done

代码示例

# while循环示例一:
[root@head test]# cat test.sh 
#!/usr/bin/bash

i=0
while (($i<10))
do
    echo $i
    let i++
done

# while循环示例二,读文件:
[root@head test]# cat test.sh 
#!/usr/bin/bash

i=0

while read line 
do
    let i++
    echo "第${i}行:$line"
done</etc/passwd

# until循环示例一:
[root@head test]# cat test.sh 
#!/usr/bin/bash

i=0
until (($i==5)) # 当i增加到5时,跳出循环
do
    let i++
    echo $i
done

for循环

基本语法

for 变量名 in [循环列表]
do
    循环体
done

# for循环默认以空格作为分隔符,可以在脚本中使用IFS指定分隔符,如IFS=$'\n'

代码示例

# for循环示例一,读文件:
[root@head test]# cat test.sh 
#!/usr/bin/bash

i=0
for line in `cat /etc/passwd`
do
    let i++
    echo "第${i}行:$line"
done

# for循环示例二:
[root@head test]# cat test.sh 
#!/usr/bin/bash

for i in {1..10}
do
    if [ $i -eq 5 ];then
        echo "pass"
        continue
    fi
    echo $i
done

# for循环示例三:
[root@head test]# cat test.sh 
#!/usr/bin/bash

for ((i=1;i<10;i++))
do
    echo $i
    if [ $i -eq 5 ];then
        echo "pass"
        break
    fi
done

 

posted on 2023-05-23 22:24  fdsimin  阅读(8)  评论(0编辑  收藏  举报