Linux5:Shell编程——流程控制

目录

前言

一、if-else

二、for循环

三、while语句、无限循环和until循环

1.while语句

2.无限循环

3.until循环

四、case ... esac

五、跳出循环

1.break

2.continue循环

总结


前言

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

 

一、if-else

  • if结构的几种写法
# if 控制语句

# if判断
# if command;
# then
#     code
# fi


# if else 语句 
# if command;
# then
#     code
# else
#     code
# fi


# if elif else 语句
# if command;
# then   
#     code
# elif command2;
# then
#     code
# *n
# else
#     code
# fi
  • 举例
#!/bin/sh

if [ `ps -ef | grep -c "ssh"` -gt 1 ];   # ps -ef 列出当前系统中所有的进程信息。
                                         # grep -c "ssh" 统计这些进程信息中包含字符串 "ssh" 的行数。
then
    echo "True"
fi

echo "-----------------"
if ((10>29));
then 
    echo "10 bigger then 20"
else
    echo "10 less then 20"
fi

echo "------------------"
# elif
a=10
b=20
if [ $a -eq $b ];
then 
    echo "a=b"
elif [ $a -gt $b ];
then
    echo  "a>b"
else [ $a -lt $b ];
    echo "a<b"
fi
  • 输出
True
-----------------
10 less then 20
------------------
a<b

 

二、for循环

  • 结构
# for循环
# for i in xxx;
# do 
#     code
# done
  • 举例
#!/bin/sh

for i in "I\'m good at coding";
do 
    echo $i
done

for i in 1 2 3 4 5 ;
do 
    echo the value is $i
done

for i in "$@";         # 文件传参 $@ 循环获取传入的参数
do 
    echo $i
done
  • 输出
[root@tokyo001 shell_code]# sh demo10.sh 2 3 4 5 5  # 文件传参写法
I\'m good at coding
the value is 1
the value is 2
the value is 3
the value is 4
the value is 5
2
3
4
5
5

 

三、while语句、无限循环和until循环

1.while语句

  • 结构
# while循环
# while codition
# do 
#     code
# done
  • 举例:注意这里出现了shell编程里的自增语句 let "n++"
#!/bin/sh

a=1
while [ $a -le 5 ]
do 
    echo $a
    let "a++"   # 等价于 a=`expr $a + 1 `
done
  • 输出
1
2
3
4
5

 

2.无限循环

  • 结构
#无限循环语法格式
# while :   # 或者 while True
# do 
#     echo "im handsome"
# done

# for ((;;))
  • 举例
#!/bin/sh

# 死循环
echo "请按ctrl+c结束"
echo -n "请输入你喜欢的电影:"     # -n 不要在输出末尾添加换行符
while read film
do 
    echo "${film}是一部好电影"
done
  • 输出:程序无限循环 按ctrl + c可退出
请按ctrl+c结束
请输入你喜欢的电影:hhh
hhh是一部好电影
xxxx
xxxx是一部好电影
gggg
gggg是一部好电影

 

3.until循环

  • 举例
#!/bin/sh

# until循环 直到条件为真为止
a=0
until [ ! $a -lt 10 ]
do 
    echo $a
    let "a++"
done
  • 输出
0
1
2
3
4
5
6
7
8
9

 

四、case ... esac

  • 举例
#!/bin/sh
# case esac  模式匹配


echo "请输入1-4之间的数字"
read -p "请输入数字:" num
case $num in
    1)
        echo "you choose 1"
        ;;   # 表示匹配结束
    2)
        echo "you choose 2"
        ;;
    3)
        echo "you choose 3"
        ;;
    4)
        echo "you choose 4"
        ;;
    *)   # 8 通配其他结果
        echo "you choose others"
        ;;  
esac

echo "----------------"
a="hhh"
case $a in 
    "hhh")
        echo "you are hhhhhhhhh"
        ;;
    "xxx")
        echo "you are xxxxxxxxx"
        ;;
    "ggg")
        echo "you are gggggggggg"
        ;;
esac
  • 输出:一种是获取用户输入的数据进行判断,一种是直接判断
请输入1-4之间的数字
请输入数字:2
you choose 2
----------------
you are hhhhhhhhh

 

五、跳出循环

1.break

  • break 命令允许跳出所有循环(终止执行后面的所有循环)
  • 举例
#!/bin/sh

# break 跳出循环
echo "welcome to paris"
echo "plese tell me yuor age"
while :
do 
    read -p "plese tell me yuor age of ten " age
    case $age in
        1|2|3|4)
            echo "your are so yong"
            ;;
        *)
            echo "old man"
            break          # 跳出整个循环
            ;;
    esac
done
  • 输出
welcome to paris
plese tell me yuor age
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 5
old man

 

2.continue循环

  • continue 命令,仅跳出当前循环
  • 举例
#!/bin/sh

# continue 跳出循环
echo "welcome to paris"
echo "plese tell me yuor age"
while :
do 
    read -p "plese tell me yuor age of ten " age
    case $age in
        1|2|3|4)
            echo "your are so yong"
            ;;
        *)
            echo "old man"
            continue        # 跳出当前循环
            ;;
    esac  
done
  • 输出:用户输入1,2,3,4以外的数字仍不会跳出整个循环,只跳出了这一次的循环
welcome to paris
plese tell me yuor age
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 2
your are so yong
plese tell me yuor age of ten 3
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 6
old man
plese tell me yuor age of ten 6
old man
plese tell me yuor age of ten 

 

总结

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

        重要的事情说三遍哈哈哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃什么芹菜卷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值