Linux Shell从入门到精通之控制与循环语句【if&while&for】

目录

1.前言🍚

2.if语句🍚

3.while语句🍚

 4.for语句🍚


1.前言

在Linux中,控制与循环语句是必不可少的,通过条件判断与循环执行可以写出丰富的shell语言脚本。下面是针对三个语句的基础介绍:

【1】if语句

if条件判断是一种常见的流程控制语句,它用于根据条件判断是否执行特定的命令或代码块。if条件判断通常由if、elif(可选)和else(可选)三个关键字组成,其中if后面紧跟一个条件语句,elif和else后面是一些可选的语句块。在if条件判断中,如果if后面的条件成立,则执行if后面的语句块,否则执行elif或else后面的语句块。

【2】while语句

while语句是一个重复执行一段代码的循环结构,当指定的条件为真时,它会一直执行括号内的语句直到条件为假。

【3】for语句

for语句是一种重复执行某个指令的结构,用于遍历列表或者执行特定的次数。

2.if语句

  • 语法格式
=======================
+     if 语法格式      +
=======================
if condition
then
    command1 
    command2
    ...
    commandN 
fi

=======================
+   if else 语法格式   +
=======================
if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

========================
+if else-if else语法格式+
========================
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi
  • if语句中的整数比较、文件比较、字符对比

整数比较:

-eq<——> 等于

-ne<——>不等

-gt<——>大于

-lt<——>小于

-ge<——>大于

-le<——>-小于

文件比较:

-e 文件或目录是否存在 [ -e file ]

-s 文件存在且至少有一个字符则为真 [ -s file ]

-d 目录是否存在 [ -d file ]

-f 文件是否存在 [ -f file ]

-r 文件存在且可读 [ -r file ]

-w 文件存在且可写 [ -w file ]

-x 文件存在且可执行 [ -x file ]

字符比较:

= 等于则为真 [ "$a" == "$b" ]

!= 不等则为真 [ ! "$b" == "$a" ]

-z 字符长度为零则为真 [ -z "$a" ]

-n 字符长度不为零则为真 [ -n "$a" ]

str1>str2 str1大于str2则为真 [ str1>str2 ]

str1<str2 str1小于str2则为真 [ str1<str2 ]

  •  整数比较运行示例
[root@CentOS_7 ~]# a=10
[root@CentOS_7 ~]# b=20
[root@CentOS_7 ~]# if [ $a == $b ]
> then
>    echo "a 等于 b"
> elif [ $a -gt $b ]
> then
>    echo "a 大于 b"
> elif [ $a -lt $b ]
> then
>    echo "a 小于 b"
> else
>    echo "没有符合的条件"
> fi
a 小于 b
[root@CentOS_7 ~]# 


PS:使用 ((...)) 作为判断语句:

[root@CentOS_7 ~]# a=10
[root@CentOS_7 ~]# b=20
[root@CentOS_7 ~]# if (( $a == $b ))
> then
>    echo "a 等于 b"
> elif (( $a > $b ))
> then
>    echo "a 大于 b"
> elif (( $a < $b ))
> then
>    echo "a 小于 b"
> else
>    echo "没有符合的条件"
> fi
a 小于 b
  •  文件比较运行示例 
[root@CentOS_7 shell-sh]# cat  -n file.sh
     1  #!/bin/bash
     2  if [ -e /etc/ssh/sshd_config ];then
     3      echo "/etc/ssh/sshd_config存在"
     4  else
     5      echo "/etc/ssh/sshd_config不存在"
     6  fi
     7
[root@CentOS_7 shell-sh]# ./file.sh
/etc/ssh/sshd_config存在
[root@CentOS_7 shell-sh]#
  •  字符比较运行示例 
[root@CentOS_7 shell-sh]# cat -n shell.sh
     1
     2  #!/bin/bash
     3  happiness="towards the sea, with spring flowers blossoming,the the"
     4
     5  echo $happiness
     6
     7  cat <<EOF
     8  1) 打印happiness长度
     9  2) 删除所有的the
    10  3) 替换第一个the为that
    11  4) 替换所有the为that
    12  EOF
    13
    14  read -p "请输入数字 1|2|3|4,或    q|Q: " var
    15
    16  if [ $var == q ];then   //字符比较
    17      exit
    18  fi
    19
    20  if [ $var == Q ];then   //字符比较
    21      exit
    22  fi
    23
    24  if [ $var -eq 1 ];then
    25      echo "当前happiness变量的长度是:${#happiness}"
    26  fi
    27
    28  if [ $var -eq 2 ];then
    29      echo ${happiness//the/}
    30  fi
    31
    32  if [ $var -eq 3 ];then
    33      echo ${happiness/the/that}
    34  fi
    35
    36  if [ $var -eq 4 ];then
    37      echo ${happiness//the/that}
    38  fi

3.while语句

  • 语法格式

while 循环用于不断执行一系列命令,也用于从输入文件中读取数据。

while condition
do
    command
done
  • 简单示例--->打印从1到5
[root@CentOS_7 shell-sh]# int=1
[root@CentOS_7 shell-sh]# while(( $int<=5 ))
> do
>     echo $int
>     let "int++"
> done
1
2
3
4
5
[root@CentOS_7 shell-sh]#
  • 简单示例---->while读取键盘输入
[root@CentOS_7 shell-sh]# cat -n while.sh
     1  #!/bin/bash
     2  echo '按下 <CTRL-D> 退出'
     3  echo -n '输入你最喜欢的网站名: '
     4  while read FILM
     5  do
     6      echo "是的!$FILM 是一个好网站"
     7  done
[root@CentOS_7 shell-sh]# ./while.sh
按下 <CTRL-D> 退出
输入你最喜欢的网站名: www.baidu.com
是的!www.baidu.com 是一个好网站
[root@CentOS_7 shell-sh]#
  • 简单示例---->无限循环

无限循环语法格式:

while :
do
    command
done
或者

while true
do
    command
done

 //慎用

while true
do
    ehco "这是一个无限循环语句!"
done
  • 循环跳出

exit 退出脚本,剩下的命令不执行。

break 跳出循环,但会执行循环后面的命令。

continue 结束本次循环,但继续下次循环,包括循环后面的命令。

  • 循环跳出示例——exit
#!/bin/bash

while true; do
    echo "请输入一个数字:"
    read num

    if [ $num -eq 0 ]; then
        exit 0
    fi

    echo "您输入的数字是: $num"
 done

这个示例中,当用户输入的数字为0时,脚本会立即通过exit语句终止执行。

  • 循环跳出示例——break
#!/bin/bash

count=1

while [ $count -le 10 ]; do
    echo "当前数字:$count"

    if [ $count -eq 5 ]; then
        break
    fi

    count=$((count+1))
done

echo "循环结束"

在这个示例中,当count变量的值为5时,循环会通过break语句跳出,然后打印出"循环结束"。

  • 循环跳出示例——continue
#!/bin/bash

count=1

while [ $count -le 10 ]; do
    if [ $((count % 2)) -eq 0 ]; then
        count=$((count+1))
        continue
    fi

    echo "奇数:$count"
    count=$((count+1))
done

echo "循环结束"

在这个示例中,当count变量的值为偶数时,循环会通过continue语句跳过后续代码,直接开始下一次循环。只有当count为奇数时,才会打印出相应的信息。循环结束后会打印出"循环结束"。

 4.for语句

  • 语法格式
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
  •  简单示例---->数字遍历
[root@CentOS_7 ~]# for loop in 1 2 3 4 5
> do
>     echo "The value is: $loop"
> done
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
  •  简单示例---->字符遍历
[root@CentOS_7 ~]# for str in This is a string
> do
>     echo $str
> done
This
is
a
string
[root@CentOS_7 ~]#
  • 简单示例---->自增与自减
###自增
[root@CentOS_7 ~]# for ((i=1; i<=10; i++)); do
>     echo "当前数字:$i"
> done
当前数字:1
当前数字:2
当前数字:3
当前数字:4
当前数字:5
当前数字:6
当前数字:7
当前数字:8
当前数字:9
当前数字:10

###自减
[root@CentOS_7 ~]# for ((i=10; i>=1; i--)); do
>     echo "当前数字:$i"
> done
当前数字:10
当前数字:9
当前数字:8
当前数字:7
当前数字:6
当前数字:5
当前数字:4
当前数字:3
当前数字:2
当前数字:1

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值