一、 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))dodone
for 语句演示
[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bashfor NUM in 1 2 3doecho $NUMdone[root@desktop27 mnt]# sh for.sh 123[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bashfor NUM in {1..3}doecho $NUMdone[root@desktop27 mnt]# sh for.sh 123[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bashfor NUM in `seq 1 3`doecho $NUMdone[root@desktop27 mnt]# sh for.sh 123[root@desktop27 mnt]#[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bashfor NUM in `seq 1 2 5`doecho $NUMdone[root@desktop27 mnt]# sh for.sh 135[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bashfor ((NUM=1;NUM<=3;NUM++))doecho $NUMdone[root@desktop27 mnt]# sh for.sh 123[root@desktop27 mnt]#
for 语句——备份数据库
[root@desktop27 mnt]# yum install mariadb-server -y[root@desktop27 mnt]# systemctl start mariadb[root@desktop27 mnt]# mysql -uroot##进行数据库设置[root@desktop27 mnt]# mysql -uroot -e "show databases;"+--------------------+| Database |+--------------------+| information_schema || linux || mysql || performance_schema || test|| westos |+--------------------+[root@desktop27 mnt]# mysql -uroot -EN -e "show databases;"*************************** 1. row ***************************information_schema*************************** 2. row ***************************linux*************************** 3. row ***************************mysql*************************** 4. row ***************************performance_schema*************************** 5. row ***************************test*************************** 6. row ***************************westos[root@desktop27 mnt]# vim dump_mysql.sh[root@desktop27 mnt]# cat dump_mysql.sh #!/bin/bashDATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`mkdir -p /mnt/mysql_dumpfor DATABASE_NAME in $DATABASE_MESSAGEdo mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql [ "$?" -eq "0" ]&&{echo -e "\033[32m$DATABASE_NAME is backuped !!\033[0m" }done[root@desktop27 mnt]# sh dump_mysql.sh linux is backuped !!mysql is backuped !!test is backuped !!westos is backuped !![root@desktop27 mnt]# ls mysql_dump/linux.sql mysql.sql test.sql westos.sql[root@desktop27 mnt]#
二、while
命令语法如下:
while test_commod ;douser_commods;done
只要test_commod命令返回0,则执行user_commods命令块;
while循环结束后,整个命令块的返回值,为user_commods命令最后一个命令的返回值,如果user_commods命令块没有执行,则整个返回值为0
while语句演示
##两种写法,效果一样[root@desktop27 mnt]# vim while.sh[root@desktop27 mnt]# cat while.sh #!/bin/bashwhile truedoecho -n `uptime`echo -ne "\r \r"sleep 55秒刷新一次done[root@desktop27 mnt]# sh while.sh ^C:14:09 up 30 min, 2 users, load average: 0.00, 0.01, 0.05[root@desktop27 mnt]#[root@desktop27 mnt]# cat while.sh #!/bin/bashwhile truedoecho -ne "\r`uptime` \r"sleep 5done[root@desktop27 mnt]# sh while.sh ^C2:18:52 up 34 min, 2 users, load average: 0.07, 0.07, 0.05 [root@desktop27 mnt]#
三、if
命令语法如下:
ifthenelifthen...eliftheneslefi
IF 语法演示
[root@desktop27 mnt]# vim if.sh [root@desktop27 mnt]# cat if.sh #!/bin/bashif[ "$1" = "a" ]thenecho '$1' is aelif[ "$1" = "b" ]thenecho '$1' is belif[ "$1" = "c" ]thenecho '$1' is celseecho unknown $1fi[root@desktop27 mnt]# sh if.sh a$1 is a[root@desktop27 mnt]# sh if.sh b$1 is b[root@desktop27 mnt]# sh if.sh c$1 is c[root@desktop27 mnt]# sh if.sh dunknown d[root@desktop27 mnt]# sh if.sh hunknown h[root@desktop27 mnt]#
1、字符串判断
str1 = str2当两个串有相同内容、长度时为真
str1 != str2 当串str1和str2不等时为真
-n str1 当串的长度大于0时为真(串非空)
-z str1 当串的长度为0时为真(空串)
str1 当串str1为非空时为真
2、数字的判断
int1 -eq int2两数相等为真
int1 -ne int2两数不等为真
int1 -gt int2int1大于int2为真
int1 -ge int2int1大于等于int2为真
int1 -lt int2int1小于int2为真
int1 -le int2int1小于等于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 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。
命令语法如下:
case 值 in模式1)command1command2command3;;模式2)command1command2command3;;*)command1command2command3;;esac
case工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。
case语句演示
##if——字符匹配,有先后顺序[root@desktop27 mnt]# vim if.sh [root@desktop27 mnt]# cat if.sh #!/bin/bashif[ "$1" = "dog" ]thenecho "cat"elif[ "$1" = "cat" ]thenecho "dog"elseecho -e "\033[31mERROR: unknown $1\033[0m"fi[root@desktop27 mnt]# sh -x if.sh cat+ '[' cat = dog ']'+ '[' cat = cat ']'+ echo dogdog[root@desktop27 mnt]# sh -x if.sh dog+ '[' dog = dog ']'+ echo catcat[root@desktop27 mnt]# sh -x if.sh boy+ '[' boy = dog ']'+ '[' boy = cat ']'+ echo -e '\033[31mERROR: unknown boy\033[0m'ERROR: unknown boy[root@desktop27 mnt]# ##case——匹配字符,没有先后顺序,效率更高[root@desktop27 mnt]# vim case.sh [root@desktop27 mnt]# cat case.sh #!/bin/bashcase $1 indog)echo cat;;cat)echo dog;;*)echo erroresac[root@desktop27 mnt]# sh -x case.sh cat+ case $1 in+ echo dogdog[root@desktop27 mnt]# sh -x case.sh dog+ case $1 in+ echo catcat[root@desktop27 mnt]# sh -x case.sh boy+ case $1 in+ echo errorerror[root@desktop27 mnt]#
五、expect
yum install expect -y
安装 expect 工具
expect 是自动应答命令,用于交互式命令的自动执行
spawn 是 expect 中的监控程序,其运行后会监控命令提出的交互问题
send 发送问题答案给交互命令
“\r” 表示会车
exp_continue 表示当问题不存在时继续回答下面的问题
expect eof 表示问题回答完毕后退出 expect 环境
interact 表示问题回答完毕后留在交互页面
set NAME [ lindex $argv n ] 定义变量
写成两个文件 .sh 和 .exp 的例子
[root@desktop27 mnt]# vim ask.sh [root@desktop27 mnt]# cat ask.sh #!/bin/bashread -p "What's your name: " NAMEread -p "How old are you: " AGEread -p "Which obj you study: " OBJread -p "Are you happy? " FEELecho "$NAME is $AGE's old and study $OBJ feel $FEEL"[root@desktop27 mnt]# sh ask.sh What's your name: tutuHow old are you: 18Which obj you study: linuxAre you happy? happytutu is 18's old and study linux feel happy[root@desktop27 mnt]# vim answer.exp[root@desktop27 mnt]# cat answer.exp #!/usr/bin/expectset timeout 2spawn /mnt/ask.shexpect "name:"send "tutu\r"expect "old:"send "18\r"expect "study:"send "linux\r"expect "happy:"send "happy\r"expect eof[root@desktop27 mnt]# expect answer.expspawn /mnt/ask.shcouldn't execute "/mnt/ask.sh": permission deniedwhile 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.shWhat's your name: tutuHow old are you: 18Which obj you study: linuxAre you happy? happytutu is 18's old and study linux feel happy[root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# cat answer.exp #!/usr/bin/expectset timeout 2spawn /mnt/ask.shexpect { name { send "tutu\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.shWhat's your name: tutuHow old are you: 18Which obj you study: linuxAre you happy? happytutu is 18's old and study linux feel happy[root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# cat answer.exp #!/usr/bin/expectset timeout 2set NAME [ lindex $argv 0]set AGE [ lindex $argv 1]set OBJ [ lindex $argv 2]set FEEL [ lindex $argv 3]spawn /mnt/ask.shexpect { 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 tutu 19 linux happyspawn /mnt/ask.shWhat's your name: tutuHow old are you: 19Which obj you study: linuxAre you happy? happytutu is 19's old and study linux feel happy[root@desktop27 mnt]# /mnt/answer.exp butterfly 19 linux badspawn /mnt/ask.shWhat's your name: butterflyHow old are you: 19Which obj you study: linuxAre you happy? badbutterfly is 19's old and study linux feel bad[root@desktop27 mnt]#
写成一个文件 .sh 的例子(和上面例子效果一样)
[root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# mv answer.exp answer.sh[root@desktop27 mnt]# cat answer.sh #!/bin/bash/usr/bin/expect <<EOFset timeout 2spawn /mnt/ask.shexpect { name { send "$1\r";exp_continue }old { send "$2\r";exp_continue }study { send "$3\r";exp_continue }happy { send "$4\r" }}expect eofEOF[root@desktop27 mnt]# sh answer.sh tutu 18 linux happyspawn /mnt/ask.shWhat's your name: tutuHow old are you: 18Which obj you study: linuxAre you happy? happytutu is 18's old and study linux feel happy[root@desktop27 mnt]#
六、exit、break、continue
exit n 脚本退出,退出值为 n
break 退出当前循环
continue 提前结束循环内部的命令,但不终止循环
[root@desktop27 mnt]# vim test.sh[root@desktop27 mnt]# cat test.sh #!/bin/bashfor NUM in {1..5}dowhile [ "$NUM" -eq "4" ]do continue 4 doneecho $NUMdone[root@desktop27 mnt]# sh test.sh 1235[root@desktop27 mnt]#
如果觉得《linux——shell 中常用的控制语句 for while if case expect exit break continue》对你有帮助,请点赞、收藏,并留下你的观点哦!