shell语法

一、if

(一)、单if语句 适用范围:一步判断条件,条件正确时做什么,条件错误时做什么

if 【condition】  #condition值为true or false
  then 
  commands
fi

大意:

如果条件为真
那么
执行commands
结束
例:
在这里插入图片描述

在这里插入图片描述

(二)、if-then-else语句 适用范围:两步判断,条件为真时做什么,条件为假时做什么

if [condition]
  then
         commands1
  else 
         commands1
fi

大意:

如果条件为真
那么
执行commands1
否则
执行commands2
结束

例:
在这里插入图片描述

在这里插入图片描述

(三)、if-then-elif语句 适用范围:多于两个以上的判断。

if [condition1]
  then
         commands1
elif [condition2]
     then
         commands2
 ………
else
    conmmandsx
fi

大意:

如果条件1为真
那么
执行commands1
如果条件2为真
执行commands2
以此类推n个条件及对应的命令
否则 (以上所有条件均不满足)
执行commandsx
结束
例:
在这里插入图片描述

在这里插入图片描述
四、

case $变量名in
”值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
...省略其他分支...
*)
如果变量的值都不是以上的值,则执行此程序
esac

举例:

#!/bin/bash
#判断用户输入
read -p "please choose yes/no:" -t 30 cho
case $cho in
         "yes")
                 echo "your choose is yes!"
                 ;;
         "no")   echo "your choose is no!"
                 ;;
         *)
                 echo "your choose is error!"
                 ;;
esac
[root@localhost sh]# bash case.sh
please choose yes/no:yes
your choose is yes!
[root@localhost sh]# bash case.sh
please choose yes/no:no
your choose is no!
[root@localhost sh]# bash case.sh
please choose yes/no:bcsdh
your choose is error!

二、for

(一)

for 变量 in 值1 值2 值3...
	do
		程序
	done

举例:

#!/bin/bash
#打印时间
for time in morning noon afternoon evening          
         do
            echo "this time is $time!"
         done
[root@localhost sh]# bash for1.sh
this time is morning!
this time is noon!
this time is afternoon!
this time is evening!

(二)

for ((初始值;循环控制条件;变量变化))
	do
		程序
	done

举例:

#!/bin/bash
#从1加到100

s=0
for ((i=1;i<=100;i=i+1))
    do
       s=$(($s+$i))
    done
echo "the sum of 1+2+...+100 is:$s"
[root@localhost sh]# bash for3.sh
the sum of 1+2+…+100 is:5050

举例②:

[root@localhost sh]# vim for4.sh
#!/bin/bash
#批量添加指定数量的用户
read -p "Please input user name: " -t 30 name
read -p "Please input the number of users: " -t 30 num 
read -p "Please input the password of users: " -t 30 pass
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
      then
      y=$(echo $num | sed 's/[0-9]//g')
          if [ -z "$y" ]
           then
           for (( i=1;i<=$num;i=i+1 ))
              do
                 useradd $name$i &>/dev/null
                   echo $pass | /usr/bin/passwd --stdin $name$i &>/dev/null
               done
       fi
fi
[root@localhost sh]# bash for4.sh
Please input user name: user
Please input the number of users: 5
Please input the password of users: 000000
[root@localhost sh]# cat /etc/passwd
user1:1000:1000::/home/user1:/bin/bash
user2:1001:1001::/home/user2:/bin/bash
user3:1002:1002::/home/user3:/bin/bash
user4:1003:1003::/home/user4:/bin/bash
user5:1004:1004::/home/user5:/bin/bash

三、while

while [ 条件判断式 ]
   do
       程序
   done

举例:

[root@localhost sh]# vim while.sh
#!/bin/bash
#从1加到100
i=1
s=0
while [ $i -le 100 ]
#如果变量i的值小于等于100.则执行循环
        do
                    s=$(( $s+$i ))
                    i=$(( $i+1 ))
        done
echo "the sum is: $s"
[root@localhost sh]# bash while.sh
the sum is: 5050

until

until [ 条件判断式 ]
do
       程序
done

举例:

#!/bin/bash
#从1加到100
i=1
s=0
until [ $i -gt 100 ]
#循环直到变量i的值大于100,就停止循环
       do
            s=$(( $s+$i ))
            i=$(( $i+1 ))
       done
echo "The sum is: $s"
[root@localhost sh]# bash until.sh
The sum is: 5050
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值