shell 中常用的控制语句 for、while、if、case、expect、exit、break、continue

一、 for 语句
命令语法如下:

for NUM in 1 2 3 
for NUM in {1..3}
for NUM in `seq 1 3`或者for NUM in `seq 1 2 10`
for (( 表达式1;表达式2;表达式3))
do
done

for 语句演示

[root@136 mnt]# vim ceshi.sh
[root@136 mnt]# cat ceshi.sh 
#!/bin/bash
for NUM in 1 2 3
do
    echo $NUM
done
[root@136 mnt]# sh ceshi.sh 
1
2
3
[root@136 mnt]# vim ceshi.sh
[root@136 mnt]# cat ceshi.sh 
#!/bin/bash
for NUM in {1..3}
do
    echo $NUM
done
[root@136 mnt]# sh ceshi.sh 
1
2
3
[root@136 mnt]# vim ceshi.sh
[root@136 mnt]# cat ceshi.sh 
#!/bin/bash
for NUM in `seq 1 2 3`
do
    echo $NUM
done
[root@136 mnt]# sh ceshi.sh 
1
3
[root@136 mnt]# vim ceshi.sh
[root@136 mnt]# cat ceshi.sh 
#!/bin/bash
for NUM in `seq 1 2 5`
do
    echo $NUM
done
[root@136 mnt]# sh ceshi.sh 
1
3
5
[root@136 mnt]# vim ceshi.sh
[root@136 mnt]# cat ceshi.sh 
#!/bin/bash
for ((NUM=1;NUM<=3;NUM++))
do
    echo $NUM
done
[root@136 mnt]# sh ceshi.sh 
1
2
3

用for语句脚本备份数据库

[root@136 mnt]# systemctl start mariadb
[root@136 mnt]# mysql -uroot -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@136 mnt]#  mysql -uroot -EN -e "show databases;"
*************************** 1. row ***************************
information_schema
*************************** 2. row ***************************
mysql
*************************** 3. row ***************************
performance_schema
*************************** 4. row ***************************
test
[root@136 mnt]# vim dump_mysql.sh
[root@136 mnt]# cat dump_mysql.sh 
#!/bin/bash
DATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysql_dump
for DATABASE_NAME in $DATABASE_MESSAGE
do
    mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql
    [ "$?" -eq "0" ]&&{
        echo -e "\033[32m$DATABASE_NAME is backuped !!\033[0m"
    }
done
[root@136 mnt]# sh dump_mysql.sh 
mysql is backuped !!
test is backuped !!
[root@136 mnt]# ls mysql_dump/
mysql.sql  test.sql

while
命令语法如下:

while test_commod ;do
    user_commods;
done

只要test_commod命令返回0,则执行user_commods命令块;
while循环结束后,整个命令块的返回值,为user_commods命令最后一个命令的返回值,如果user_commods命令块没有执行,则整个返回值为0
演示

[root@136 mnt]# vim while.sh
[root@136 mnt]# cat while.sh 
#!/bin/bash
while true
do
    echo -n `uptime`
    echo -ne "\r    \r"
    sleep 55s刷新
done
[root@136 mnt]# sh while.sh 
    3:06 up 1:00, 3 users, load average: 0.01, 0.14, 0.13

if
命令语法如下:

if
then
elif
then
.
.
.
elif
then
esle
fi

if语法演示

[root@136 mnt]# vim if.sh
[root@136 mnt]# cat if.sh 
#!/bin/bash
if
[ "$1" = "a" ]
then
    echo '$1' is a
elif
[ "$1" = "b" ]
then
    echo '$1' is b
elif
[ "$1" = "c" ]
then
    echo '$1' is c
else
    echo unknown $1
fi
[root@136 mnt]# sh if.sh a
$1 is a
[root@136 mnt]# sh if.sh b
$1 is b
[root@136 mnt]# sh if.sh h
unknown h
常用判断
1、字符串判断
str1 = str2      当两个串有相同内容、长度时为真
str1 != str2      当串str1和str2不等时为真
-n str1        当串的长度大于0时为真(串非空)
-z str1        当串的长度为0时为真(空串)
str1         当串str1为非空时为真
2、数字的判断
int1 -eq int2    两数相等为真
int1 -ne int2    两数不等为真
int1 -gt int2    int1大于int2为真
int1 -ge int2    int1大于等于int2为真
int1 -lt int2    int1小于int2为真
int1 -le int2    int1小于等于int2为真
3、文件的判断
-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真
4、复杂逻辑判断
-a         与
-o        或
!        非

case
case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。
命令语法如下:

casein
模式1)
    command1
    command2
    command3
;;
模式2)
    command1
    command2
    command3
;;
*)
    command1
    command2
    command3
;;
esac

case工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

[root@136 mnt]# vim if.sh
[root@136 mnt]# cat if.sh 
#!/bin/bash
if
[ "$1" = "dog" ]
then
    echo "cat"
elif
[ "$1" = "cat" ]
then
    echo "dog"
else
    echo -e "\033[31mERROR: unknown $1\033[0m"
fi

[root@136 mnt]# sh -x if.sh cat
+ '[' cat = dog ']'
+ '[' cat = cat ']'
+ echo dog
dog
[root@136 mnt]# sh -x if.sh dog
+ '[' dog = dog ']'
+ echo cat
cat
[root@136 mnt]# sh -x if.sh apple
+ '[' apple = dog ']'
+ '[' apple = cat ']'
+ echo -e '\033[31mERROR: unknown apple\033[0m'
ERROR: unknown apple
[root@136 mnt]# vim case.sh
[root@136 mnt]# cat case.sh 
#!/bin/bash
case $1 in
    dog)
    echo cat
    ;;
    cat)
    echo dog
    ;;
    *)
    echo error
esac
[root@136 mnt]# sh -x case.sh cat
+ case $1 in
+ echo dog
dog
[root@136 mnt]# sh -x case.sh dog
+ case $1 in
+ echo cat
cat
[root@136 mnt]# sh -x case.sh apple
+ case $1 in
+ echo error
error

expect
expect 是自动应答命令,用于交互式命令的自动执行
spawn 是 expect 中的监控程序,其运行后会监控命令提出的交互问题
send 发送问题答案给交互命令
“\r” 表示会车
exp_continue 表示当问题不存在时继续回答下面的问题
expect eof 表示问题回答完毕后退出 expect 环境
interact 表示问题回答完毕后留在交互页面
set NAME [ lindex $argv n ] 定义变量
[root@136 mnt]# yum install expect.x86_64 -y

[root@136 mnt]# vim ask.sh
[root@136 mnt]# cat ask.sh 
#!/bin/bash
read -p "What's your name: " NAME
read -p "How old are you: " AGE
read -p "Which obj you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old and study $OBJ feel $FEEL"
[root@136 mnt]# sh ask.sh 
What's your name: wb
How old are you: 18
Which obj you study: linux
Are you happy? happy
wb is 18's old and study linux feel happy
[root@136 mnt]# vim answer.exp
[root@136 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect "name:"
send "wb\r"
expect "old:"
send "18\r"
expect "study:"
send "linux\r"
expect "happy:"
send "happy\r"
expect eof
[root@desktop27 mnt]# expect answer.exp
spawn /mnt/ask.sh
couldn't execute "/mnt/ask.sh": permission denied
    while executing
"spawn /mnt/ask.sh"
    (file "answer.exp" line 2)
[root@desktop27 mnt]# chmod -x /mnt/ask.sh
[root@desktop27 mnt]# expect answer.exp 
spawn /mnt/ask.sh
What's your name: wb
How old are you: 18
Which obj you study: linux
Are you happy? happy
wb is 18's old and study linux feel happy
[root@desktop27 mnt]# vim answer.exp 
[root@desktop27 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect { 
     name { send "wb\r";exp_continue }
     old { send "18\r";exp_continue }
     study { send "linux\r";exp_continue }
     happy { send "happy\r" }
}
expect eof
[root@desktop27 mnt]# chmod +x answer.exp 
[root@desktop27 mnt]# /mnt/answer.exp 
spawn /mnt/ask.sh
What's your name: wb
How old are you: 18
Which obj you study: linux
Are you happy? happy
tutu is 18's old and study linux feel happy
[root@desktop27 mnt]# vim answer.exp 
[root@desktop27 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
set NAME [ lindex $argv 0]
set AGE  [ lindex $argv 1]
set OBJ  [ lindex $argv 2]
set FEEL [ lindex $argv 3]
spawn /mnt/ask.sh
expect { 
     name { send "$NAME\r";exp_continue }
     old { send "$AGE\r";exp_continue }
     study { send "$OBJ\r";exp_continue }
     happy { send "$FEEL\r" }
}
expect eof
[root@desktop27 mnt]# /mnt/answer.exp wb 18 linux happy
spawn /mnt/ask.sh
What's your name: wb
How old are you: 18
Which obj you study: linux
Are you happy? happy
wb is 18's old and study linux feel happy
[root@desktop27 mnt]# /mnt/answer.exp wangbo 20 linux bad
spawn /mnt/ask.sh
What's your name: wangbo
How old are you: 20
Which obj you study: linux
Are you happy? bad
wangbo is 20's old and study linux feel bad

用sh实现

[root@136 mnt]# vim answer.sh
[root@136 mnt]# cat answer.sh 
#!/bin/bash
/usr/bin/expect <<EOF
set timeout 2
spawn /mnt/ask.sh
expect { 
     name { send "$1\r";exp_continue }
     old { send "$2\r";exp_continue }
     study { send "$3\r";exp_continue }
     happy { send "$4\r" }
}
expect eof
EOF
[root@136 mnt]# sh answer.sh ha 20 linux happy
spawn /mnt/ask.sh
What's your name: ha
How old are you: 20
Which obj you study: linux
Are you happy? happy
ha is 20's old and study linux feel happy

exit、break、continue

exit n 脚本退出,退出值为 n
break 退出当前循环
continue 提前结束循环内部的命令,但不终止循环

[root@136 mnt]# vim test.sh
[root@136 mnt]# cat test.sh 
#!/bin/bash
for NUM in {1..5}
do
    while 
    [ "$NUM" -eq "4" ]
    do  
        continue 4 
    done
    echo $NUM
done
[root@136 mnt]# sh test.sh 
1
2
3
5
[root@136 mnt]# 

猜你喜欢

转载自blog.csdn.net/awoyaoc/article/details/80847794
今日推荐