与其他语句的区别:
case 语句和 if…elif…else 语句一样都是多分支条件语句,不过和
if 多分支条件语句不同的是,case 语句只能判断一种条件关系,
而 if 语句可以判断多种条件关系。
语法:
例句:判断用户输入
[root@localhost ~]# mkdir /opt/sh
[root@localhost ~]# cd /opt/sh
[root@localhost sh]# vim case.sh
#!/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!
语法一:
例1:
[root@localhost sh]# vim for1.sh
#!/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!
例2:
[root@localhost sh]# vim for2.sh
#!/bin/bash
#批量解压脚本
cd /opt
ls *.tar.gz > ls.log
for i in $(cat ls.log)
do
tar -zxf $i &>/dev/null
done
rm -rf/opt/ls.log
[root@localhost opt]# ll
total 168
-rw-r–r-- 1 root root 168639 Apr 20 14:39 lnmp1.7.tar.gz
drwxr-xr-x 2 root root 48 Apr 20 14:43 sh
[root@localhost opt]# cd /opt/sh
[root@localhost sh]# ll
total 12
-rw-r–r-- 1 root root 314 Apr 20 10:56 case.sh
-rw-r–r-- 1 root root 136 Apr 20 11:12 for1.sh
-rw-r–r-- 1 root root 156 Apr 20 14:43 for2.sh
[root@localhost sh]# bash for2.sh
[root@localhost sh]# cd /opt
[root@localhost opt]# ll
total 172
drwxr-xr-x 7 root root 4096 Jul 15 2020 lnmp1.7
-rw-r–r-- 1 root root 168639 Apr 20 14:39 lnmp1.7.tar.gz
drwxr-xr-x 2 root root 48 Apr 20 14:43 sh
语法二:
例1:数字相加
[root@localhost sh]# vim for3.sh
#!/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
例2:添加指定数量的用户
[root@localhost sh]# vim for4.sh
[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
/usr/sbin/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循环是不定循环,也称作条件循环 。只要条件判断式成立,循环就会一直继 续,直到条件判断式不成立,循环才会停
止。这就和for的固定循环不太一样了。
语句:
while [ 条件判断式 ]
do
程序
done
例题:1加到100
[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循环,和while循环相反,until循环时 只要条件判断式不成立则进行循环,并执 行循环程序。一旦循环条件成立,则终止循环。
语句:
until [ 条件判断式 ]
do
程序
done
例题:1加到100
#!/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