一、 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@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh
#!/bin/bash
for NUM in 1 2 3
doecho $NUM
done
[root@desktop27 mnt]# sh for.sh
1
2
3
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh
#!/bin/bash
for NUM in {1..3}
doecho $NUM
done
[root@desktop27 mnt]# sh for.sh
1
2
3
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 3`
doecho $NUM
done
[root@desktop27 mnt]# sh for.sh
1
2
3
[root@desktop27 mnt]#
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 2 5`
doecho $NUM
done
[root@desktop27 mnt]# sh for.sh
1
3
5
[root@desktop27 mnt]# vim for.sh
[root@desktop27 mnt]# cat for.sh
#!/bin/bash
for ((NUM=1;NUM<=3;NUM++))
doecho $NUM
done
[root@desktop27 mnt]# sh for.sh
1
2
3
[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/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@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/bash
while true
doecho -n `uptime`echo -ne "\r    \r"sleep 5     5秒刷新一次
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/bash
while true
doecho -ne "\r`uptime`    \r"sleep 5
done
[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

命令语法如下:

if
then
elif
then
.
.
.
elif
then
esle
fi

IF 语法演示

[root@desktop27 mnt]# vim if.sh
[root@desktop27 mnt]# cat if.sh
#!/bin/bash
if
[ "$1" = "a" ]
thenecho '$1' is a
elif
[ "$1" = "b" ]
thenecho '$1' is b
elif
[ "$1" = "c" ]
thenecho '$1' is c
elseecho unknown $1
fi
[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 d
unknown d
[root@desktop27 mnt]# sh if.sh h
unknown 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 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 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。

命令语法如下:

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/bash
if
[ "$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 dog
dog
[root@desktop27 mnt]# sh -x if.sh dog
+ '[' dog = dog ']'
+ echo cat
cat
[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/bash
case $1 indog)echo cat;;cat)echo dog;;*)echo error
esac
[root@desktop27 mnt]# sh -x case.sh cat
+ case $1 in
+ echo dog
dog
[root@desktop27 mnt]# sh -x case.sh dog
+ case $1 in
+ echo cat
cat
[root@desktop27 mnt]# sh -x case.sh boy
+ case $1 in
+ echo error
error
[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/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@desktop27 mnt]# sh ask.sh
What's your name: tutu
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
spawn /mnt/ask.sh
expect "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.exp
spawn /mnt/ask.sh
couldn'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.sh
What's your name: tutu
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
spawn /mnt/ask.sh
expect { 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.sh
What's your name: tutu
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 tutu 19 linux happy
spawn /mnt/ask.sh
What's your name: tutu
How old are you: 19
Which obj you study: linux
Are you happy? happy
tutu is 19's old and study linux feel happy
[root@desktop27 mnt]# /mnt/answer.exp butterfly 19 linux bad
spawn /mnt/ask.sh
What's your name: butterfly
How old are you: 19
Which obj you study: linux
Are you happy? bad
butterfly 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 <<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@desktop27 mnt]# sh answer.sh tutu 18 linux happy
spawn /mnt/ask.sh
What's your name: tutu
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]#

六、exit、break、continue

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

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

linux——shell 中常用的控制语句 for、while、if、case、expect、exit、break、continue相关推荐

  1. linux+if语句+break,linux——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` f ...

  2. linux if else 格式,linux shell中 if else以及大于、小于、等于逻辑表达式

    在linux shell编程中,大多数情况下,可以使用测试命令来对条件进行测试,这里简单的介绍下,方便需要的朋友 比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示 ...

  3. linux脚本除号,shell中常用的特殊符号整理

    在shell中常用的特殊符号罗列如下: # ; ;; . , / \ 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `co ...

  4. Day25 linux shell中的特殊符号与命令

    2019独角兽企业重金招聘Python工程师标准>>> linux shell中的特殊符号 *:代表零个或多个任意字符 ?:只代表一个任意字符,不管是数字还是字母,只要是一个字符,都 ...

  5. linux shell数学计算器,技术|使用 GNU bc 在 Linux Shell 中进行数学运算

    在 shell 中使用 bc 更好地做算数,它是一种用于高级计算的数学语言. 大多数 POSIX 系统带有 GNU bc,这是一种任意精度的数字处理语言.它的语法类似于 C,但是它也支持交互式执行语句 ...

  6. linux——shell 中的运算

    运算方式及运算符号 运算符号 意义 +,- 加法,减法 *,/,% 乘法,除法,取余 ** 幂运算 ++,- - 自增加,自减少 <,<=,>,>= 比较符号 =,+=,-=, ...

  7. linux中bc用法英文,使用GNU bc在Linux Shell中进行数学运算

    在 shell 中使用 bc 更好地做算数,它是一种用于高级计算的数学语言. 大多数 POSIX 系统带有 GNU bc,这是一种任意精度的数字处理语言.它的语法类似于 C,但是它也支持交互式执行语句 ...

  8. Linux系统中常用命令行命令、快捷键、创建长路径的快捷键

    Linux系统中常用命令行命令.快捷键.创建长路径的快捷键 1.常用Linux命令: 1.1 文件和目录: cd /home 进入 '/home' 目录 cd - 返回上一级目录 cd -/- 返回上 ...

  9. linux判断目录是否存在命令,linux shell 中判断文件、目录是否存在的方法

    本文主要介绍了linux shell 中判断文件.目录是否存在的方法,分享给大家 -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为 ...

最新文章

  1. 设置单元格不换行,多出的部分隐藏
  2. 「 每日一练,快乐水题 」504. 七进制数
  3. QT的QPair类的使用
  4. iphone安装Deb文件
  5. 第四章、PL/SQL基础
  6. mysql active_GitHub - vipshop/drc: MySQL active-active replication solution.
  7. Linux Ctrl+c与ctrl+z的区别
  8. 社群商业模式设计方案
  9. 不同路径(I和II)--动态规划
  10. js base64图片太大_JS实现base64图片下载 简易方法
  11. 微信云控的好处及云控与群控的差别,后续讨论微信不死号
  12. dataframe筛选列名_python 查看列名_Pandas 库之 DataFrame - Python学习笔记
  13. ChucK初步(5)
  14. mysql update convert_Oracle/云MySQL/MsSQL“大迁移”真相及最优方案
  15. c语言中分不分大小写,C语言高级语言程序设计(一)-第四章 程序设计方法-模块化与算法设计.ppt...
  16. 【新观点】孙悟空其实是太上老君炼的丹药变成的
  17. 下载单词英语单词_单词如何塑造体验
  18. python decimal_实例详解Python模块decimal
  19. 软件定义的容错计算机体系,1.4 软件容错技术 - 计算机系统容错技术方法
  20. python asyncio_Python 异步 IO系列:认识asyncio

热门文章

  1. 《他其实没那么喜欢你》经典台词(4)
  2. 订单BOM与销售BOM的区别
  3. 【MM】采购退货的处理办法
  4. SD--订单最小量限制的增强
  5. 2020前三季度各省市人均收入来了!看看你的家乡排第几?
  6. mysql5.7配置innodb_MySQL_5.7新特性innodb-buffer-pool-size配置
  7. java中为什么要用json_Java中使用JSON
  8. Java 怎么 get char_Java KeyCharacterMap.getDeadChar方法代码示例
  9. 制作碳排放强度的空间可视化_【科研成果】吴传清、宋子逸:长江经济带农业碳排放的时空差异特征分析...
  10. (pwn) C语言 write函数且使用write函数泄露 libc版本