3 case-for-while

1case流程控制语句

用来实现对程序流程的选择,循环,转向和返回等进行控制,case是开关语句的一个组成部分
它是根据变量的取值不同,进行比较,然后针对不同的取值分别执行不同的命令操作

语法格式

case 变量或表达式 in
    变量或表达式1)
        命令语句
        ;;
    变量或表达式2)
        命令语句
        ;;
    *)
        命令语句
esac

示例一,根据输入的值,输出不同的内容

[root@meditation ~]# cat case1.sh 
#!/bin/bash

cat <<eof
******************
1,backup
2,copy
3,quit
******************
eof

read -p "Input a choose:" OP
case $OP in
    1|backop)
    echo "BACKUP........"
    ;;
    2|copy)
    echo "COPY......."
    ;;
    3|quit)
    echo "QUIT......."
    ;;
    *)
    echo "请输入选项"
esac
[root@meditation ~]# bash case1.sh 
******************
1,backup
2,copy
3,quit
******************
Input a choose:3
QUIT.......

示例二,apache启动脚本

[root@meditation ~]# cat ./case2.sh 
#!/bin/bash

case $1 in
    start)
    systemctl $1 httpd
    ps aux | grep httpd
    echo "httpd start"
    ;;
    stop)
    systemctl $1 httpd
    ps aux | grep httpd
    echo "httpd stop"
    ;;
    status)
    systemctl $1 httpd
    ps aux | grep httpd
    echo "httpd status"
    ;;
    restart)
    systemctl $1 httpd
    ps aux | grep httpd
    echo "httpd restart"
    ;;
    *)
    echo "请输入start|stop|status|restart"
esac
[root@meditation ~]# chmod +x ./case2.sh 

[root@meditation ~]# ./case2.sh restart
root      4670  4.0  0.5 226220  5128 ?        Ss   21:43   0:00 /usr/sbin/httpd -DFOREGROUND
apache    4675  0.0  0.3 228304  3140 ?        S    21:43   0:00 /usr/sbin/httpd -DFOREGROUND
root      4676  0.0  0.1 226220  1880 ?        R    21:43   0:00 /usr/sbin/httpd -DFOREGROUND
apache    4677  0.0  0.3 228304  3140 ?        S    21:43   0:00 /usr/sbin/httpd -DFOREGROUND
apache    4678  0.0  0.3 228304  3140 ?        S    21:43   0:00 /usr/sbin/httpd -DFOREGROUND
apache    4679  0.0  0.3 228304  3140 ?        S    21:43   0:00 /usr/sbin/httpd -DFOREGROUND
httpd restart

for循环语句

语法格式

for var in list
do
    commands
done

或者

for var in list ; do
    commands
done

示例一,循环打印一组字符串

[root@meditation ~]# cat for1.sh 
#!/bin/bash
for var in a1 b1 c1 d1 "hello world"
do
    echo $var
done
[root@meditation ~]# bash ./for1.sh 
a1
b1
c1
d1
hello world

示例二,从变量中取值

[root@meditation ~]# cat for2.sh 
#!/bin/bash
list="a1 a2 a3 a4"
for i in $list
do
    echo is a $i
done
[root@meditation ~]# bash for2.sh 
is a a1
is a a2
is a a3
is a a4

示例三,从命令中取值

默认情况下,bash shell会以空格,制表符,换行符作为分隔符号,
通过IFS来自定义分隔符号,可以指定多个

  • IFS='\n':将字符\和字符n作为IFS的换行符号
  • IFS=$'\n':真正的使用换行符作为字段分隔符号
[root@meditation ~]# cat for3.sh 
#!/bin/bash
IFS=$'\n'
for i in `cat /etc/hosts`
do
    echo $i
done
[root@meditation ~]# bash for3.sh 
127.0.0.1 VM_0_15_centos VM_0_15_centos
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
::1 VM_0_15_centos VM_0_15_centos
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

c语言风格的for

语法格式

for((i=0;i<10;i++))
do
    commands
done

示例一,单个变量输出1-10

[root@meditation ~]# cat for4.sh 
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
    echo this is $i
done
[root@meditation ~]# bash for4.sh 
this is 1
this is 2
this is 3
this is 4
this is 5
this is 6
this is 7
this is 8
this is 9
this is 10

示例二,多个变量.同时输出1-9的升序和倒序

[root@meditation ~]# cat for5.sh 
#!/bin/bash
for (( a=1,z=9; a<10; a++,z-- ))
do
    echo num is $a===========$z

done
[root@meditation ~]# bash ./for5.sh 
num is 1===========9
num is 2===========8
num is 3===========7
num is 4===========6
num is 5===========5
num is 6===========4
num is 7===========3
num is 8===========2
num is 9===========1

3while循环语句和循环嵌套

只要条件成立就反复执行对应的命令操作,直到命令不成立或者为假

语法格式

while 测试命令
do 
    命令
done

示例一,降序输出数字10到1

[root@meditation ~]# cat ./while1.sh 
#!/bin/bash
var1=10
while [ $var1 -gt 0 ]
do 
    echo $var1
    var1=$[$var1-1]
done
[root@meditation ~]# bash ./while1.sh
10
9
8
7
6
5
4
3
2
1

示例二,输出如下效果

1 * 1 = 1
...
9 * 9 = 81
[root@meditation ~]# cat ./while2.sh 
#!/bin/bash

num=1
while [ $num -lt 10 ]
do
    sum=$(( $num * $num ))
    echo "$num * $num = $sum"
    ((num++))
done
[root@meditation ~]# bash ./while2.sh 
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64
9 * 9 = 81

循环嵌套

例子一,批量添加a.txt文件中的五个用户

编写脚本的思路:

  • 1明确脚本的功能
  • 2编写脚本会使用到哪些命令? useeradd passwd for
  • 3把变化的数据使用变量表示
  • 4选择适合的流程控制(选择,循环,分支)
[root@meditation ~]# cat /root/a.txt 
mk1
cd2
ls3
find4
in5
[root@meditation ~]# cat ./add.sh 
#!/bin/bash

for name in `cat /root/a.txt`
do
    id $name &> /dev/null
    if [ $? -ne 0 ];then
        useradd $name
        echo '123456' | passwd --stdin $name &> /dev/null
        echo 'user $name created'
    else
        echo 'user $name is exist'
    fi
done
[root@meditation ~]# bash ./add.sh 
user $name created
user $name created
user $name created
user $name created
user $name created
[root@meditation ~]# bash ./add.sh 
user $name is exist
user $name is exist
user $name is exist
user $name is exist
user $name is exist
[root@meditation ~]# id mk1
uid=1008(mk1) gid=1008(mk1) groups=1008(mk1)

示例二,打印久久乘法表

1*1=1
2*1=2  2*2=4
...

注:外层循环,循环行. 内层循环,循环列
规则:内层循环的变量<=外层循环的变量

[root@meditation ~]# cat ./99.sh
#!/bin/bash
for i in `seq 9`
do
    for j in `seq 9`
    do
        [ $j -le $i ] && echo -n "$i*$j=`echo $(($i*$j))`"
    done
    echo "  "
done
[root@meditation ~]# bash ./99.sh 
1*1=1  
2*1=22*2=4  
3*1=33*2=63*3=9  
4*1=44*2=84*3=124*4=16  
5*1=55*2=105*3=155*4=205*5=25  
6*1=66*2=126*3=186*4=246*5=306*6=36  
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49  
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64  
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81  

三个脚本

脚本一,将/opt目录下的所有的日志文件自动打包

[root@VM_0_15_centos ~]# cat ./packups.sh 
#!/bin/bash
SRC_DIR=/var/log/
DES_DIR=/opt/backup/`date +%Y%m%d`
if [ ! -d $DES_DIR ]; then
    mkdir -p $DES_DIR
fi
for i in `find $SRC_DIR -name "*.log"`
do
    tar czf $i.tgz $i
done
mv /var/log/*.tgz $DES_DIR
ls -lh $DES_DIR
echo "the scripts exec end, files tar successfully"
[root@VM_0_15_centos ~]# bash packups.sh 
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
total 28K
-rw-r--r-- 1 root root  120 Aug 21 21:40 boot.log.tgz
-rw-r--r-- 1 root root 9.5K Aug 21 21:40 cloud-init.log.tgz
-rw-r--r-- 1 root root 1.5K Aug 21 21:40 cloud-init-output.log.tgz
-rw-r--r-- 1 root root  136 Aug 21 21:40 qcloud_action.log.tgz
-rw-r--r-- 1 root root  187 Aug 21 21:40 yum.log.tgz
the scripts exec end, files tar successfully

脚本二,找出某个网段中,已经关机的服务器

[root@VM_0_15_centos ~]# cat ping.sh 
#!/bin/bash
i=1
for (( i=1; i<=256; i++))
do
    ping -c 3 192.168.0.$i &> /dev/null
    if [ $? -ne 0 ]; then
        echo 192.168.0.$i is shutdown
    fi
done
[root@VM_0_15_centos ~]# bash ping.sh 
192.168.0.1 is shutdown
192.168.0.2 is shutdown
192.168.0.3 is shutdown
192.168.0.4 is shutdown

脚本二,创建账号,并生成随机密码

[root@VM_0_15_centos ~]# cat useradd1.sh 
#!/bin/bash
for i in xuegods{1..10}
do
    useradd $i
    pass=`date +%s | md5sum | cut -c 1-8`
    sleep 1
    echo "$i:$pass" >> /tmp/passwd.txt
    echo $pass | passwd --stdin $i > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "create user is successfully!"
    else
        echo "create user is failed!"
    fi
done
[root@VM_0_15_centos ~]# bash useradd1.sh 
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
create user is successfully!
[root@VM_0_15_centos ~]# cat /tmp/passwd.txt 
xuegods1:930f0303
xuegods2:664c99b1
xuegods3:4d715740
xuegods4:3819217d
xuegods5:684558f2
xuegods6:e85bdffa
xuegods7:1ee778b3
xuegods8:246e39c3
xuegods9:bc6b795a
xuegods10:b38c0a98
[root@VM_0_15_centos ~]# su xuegods1
[xuegods1@VM_0_15_centos root]$ 

猜你喜欢

转载自www.cnblogs.com/inmeditation/p/12145685.html