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 "游戏结束" 永远不会被执行。

```
相关推荐
RisunJan4 分钟前
Linux命令-ngrep(方便的数据包匹配和显示工具)
linux·运维·服务器
.千余6 分钟前
【Linux】基本指令3
linux·服务器·开发语言·学习
南境十里·墨染春水18 分钟前
C++ 笔记 thread
java·开发语言·c++·笔记·学习
南境十里·墨染春水19 分钟前
C++ 笔记 高级线程同步原语与线程池实现
java·开发语言·c++·笔记·学习
热爱Liunx的丘丘人21 分钟前
Ansible-doc及常用模块
linux·运维·服务器·ansible
lkforce26 分钟前
MiniMind学习笔记(二)--model_minimind.py
笔记·python·学习·minimind·minimindconfig
SPC的存折33 分钟前
D在 Alpine 容器中手动搭建 Discuz 全攻略(包含镜像一键部署脚本,可直接用)
linux·数据库·mysql·缓存
tianyuanwo1 小时前
OS/DevOps程序员切入Harness Engineering的入门与进阶指南
运维·devops·harness
飞飞传输1 小时前
国产化FTP替代方案哪个好?选对平台让传输更安全高效
大数据·运维·安全
一生了无挂1 小时前
自己编译RustDesk,并将自建ID服务器和key信息写入客户端
运维·服务器