随笔 - 65  文章 - 0  评论 - 0  阅读 - 13429

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   fdsimin  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示