LInux:循环语句

LInux:循环语句

if-else语句

复制代码
#### if 语句语法格式

```shell
if [ $a -gt $b ];
then 
    echo "a>b"
fi

if [ $a -gt $b ];
then 
    echo "a>b"
    echo "a!=b"
    echo "true"
fi
```

if-else语句

复制代码
#### if-else 语句语法格式

```shell
if [ $a -gt $b ];
then 
    echo "a>b"
else
    echo "a<=b"
fi

if [ $a -gt $b ];
then 
    echo "a>b"
    echo "a!=b"
    echo "true"
else
    echo "a<=b"
fi

if ((a>b));
then 
    echo "true"
    echo "a>b"
else
    echo "a<=b"
fi
```
复制代码
#### if else语法格式

```shell
if [ $a -gt $b ];
then 
    echo "a>b"
elif [$a -eq $b]
then
    echo "a=b"
else
    echo "a<b"
fi
```

for 循环

复制代码
#### for循环一般格式为

```shell
for m in 1 2 3 4;
do 
   echo "values is $m"
done

for n in This is a dong;
do 
   echo $n 
done
```

while 语句

复制代码
#### while 循环语法格式

```shell
int=5
while(($int>=2));
do 
   echo "$int"
   let "int--"
done

echo "按下CTRL+D退出"
echo "输入你喜欢的电影"
while read film;
do
   echo "${film} is ok"
done
```

无限循环

shell 复制代码
while True;
do
  echo "ok"
done

for ((::))

until循环

复制代码
#### until 循环执行一系列命令直至条件为 true 时停止

```shell
a=0
until [ ! $a -lt 10 ];
do
    echo $a
    a=$((a+1))
done
```

case ... esac

shell 复制代码
echo "输入1到4之间的数字"
read -p "数字:" num
case $num in 
    1) 
        echo '1'
    ;;
    2) 
        echo '2'
    ;;
    3) 
        echo '3'
    ;;
    4) 
        echo '4'
    ;;
esac   

s1='iambot'
case $s1 in
    'iambot') echo 'bot'
    ;;
    'iampm') echo 'pm'
    ;;
    'iampl') echo 'pl'
    ;;
esac 

跳出循环

复制代码
#### break 命令允许跳出所有循环

```shell
echo "welcome to Amusement park"
echo "please inter a number between 1-4"
while :
do
    read -p "please inter your number: " num
    case $num in
        1|2|3|4)
            echo "you choose 1-4 you are great"
            ;;
        *)
            echo "you choose others you are bad"
            break
            ;;
    esac
done
```
复制代码
#### continue 命令不会跳出所有循环,仅仅跳出当前循环。

```shell
echo "welcome to Amusement park"
echo "please inter a number between 1-4"
while :
do
    read -p "please inter your number: " num
    case $num in
        1|2|3|4)
            echo "you choose 1-4 you are great"
            ;;
        *)
            echo "you choose others you are bad"
            continue
            echo 'game over'
            ;;
    esac
done
#运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行。

```
相关推荐
平平无奇。。。5 分钟前
Mysql库的操作和表的操作
linux·数据库·mysql
小小星球之旅10 分钟前
redis缓存常见问题
数据库·redis·学习·缓存
Haoea!11 分钟前
Flink03-学习-套接字分词流自动写入工具
开发语言·学习
哆啦A梦的口袋呀22 分钟前
基于Python学习《Head First设计模式》第三章 装饰者模式
python·学习·设计模式
哆啦A梦的口袋呀23 分钟前
基于Python学习《Head First设计模式》第五章 单件模式
python·学习·设计模式
一张假钞30 分钟前
Linux 下 ChromeDriver 安装
linux·运维·服务器
shark-chili33 分钟前
Java并发编程哲学系列汇总
linux·运维·服务器·操作系统
weixin_3077791337 分钟前
Neo4j 备份与恢复:原理、技术与最佳实践
运维·数据库·neo4j
qq_243050791 小时前
rtpmixsound:实现音频混音攻击!全参数详细教程!Kali Linux教程!
linux·web安全·网络安全·黑客·渗透测试·voip·kali linux
背太阳的牧羊人1 小时前
docker 中 什么是「卷」?(Volume)
运维·docker·容器