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`

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

do

echo $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}

do

echo $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`

do

echo $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`

do

echo $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++))

do

echo $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 ;do

user_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

do

echo -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

do

echo -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" ]

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@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)

command1

command2

command3

;;

模式2)

command1

command2

command3

;;

*)

command1

command2

command3

;;

esac

case工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。

取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

case语句演示

##if——字符匹配,有先后顺序

[root@desktop27 mnt]# vim if.sh

[root@desktop27 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@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 in

dog)

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 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: 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 <

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}

do

while

[ "$NUM" -eq "4" ]

do

continue 4

done

echo $NUM

done

[root@desktop27 mnt]# sh test.sh

1

2

3

5

[root@desktop27 mnt]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值