Linux Shell编程——语句

一、if 条件语句

1.结构

if
then
elif
then

else
fi

2.if 语句示例
[root@shellexample mnt]# cat test.sh 
#!/bin/bash
if [ "$1" = "yunwei" ]
then
	echo linux
elif [ "$1" = "kaifa" ]
then
	echo java
else
	echo error!!
fi
[root@shellexample mnt]# sh test.sh
error!!
[root@shellexample mnt]# sh test.sh yunwei
linux
[root@shellexample mnt]# sh test.sh kaifa
java
[root@shellexample mnt]# sh test.sh alkdl
error!!

脚本示例:
1.执行check.sh文件名 后,显示检查的文件类型;

[root@shellexample mnt]# vim check.sh
#!/bin/bash
if [ -z "$1" ]
then
	echo "ERROR:Please input a filename follow script !! "
elif [ ! -e "$1" ]
then
	echo "ERROR:$1 is not exists !"
elif [ -f "$1" ] 
then
	echo "$1 is file"
elif [ -L "$1" ]
then
	echo "$1 is link"
elif [ -S "$1" ]
then
	echo "$1 is taojiezi"
elif [ -b "$1" ]
then
	echo "$1 is block"
elif [ -d "$1" ]
then
	echo "$1 is directory"
elif [ -c "$1" ]
then
	echo "$1 is char"
else
	exit
fi

测试:

[root@shellexample mnt]# sh check.sh 
ERROR:Please input a filename follow script !! 
[root@shellexample mnt]# sh check.sh /mnt
/mnt is directory
[root@shellexample mnt]# sh check.sh jakkd
ERROR:jakkd is not exists !

二、循环语句

1.for 语句结构

for
do
done

2.for 语句示例
[root@shellexample mnt]# for NUM in 1 2 3
> do
> echo $NUM
> done
1
2
3
[root@shellexample mnt]# for NUM in {1..3}
> do
> echo $NUM
> done
1
2
3
[root@shellexample mnt]# for NUM in `seq 1 3`
> do
> echo $NUM
> done
1
2
3
[root@shellexample mnt]# for NUM in `seq 1 2 10`    ##可以设定步长,即跨度
> do
> echo $NUM
> done
1
3
5
7
9

脚本示例:
1.执行user_create.sh userfile ,如果文件里的用户存在,报错,不存在则建立;

[root@shellexample mnt]# cat userfile
westos1
westos2
westos3
[root@shellexample mnt]# vim user_create.sh
#!/bin/bash
[ -z "$1" ] &&{
        echo "ERROR:Please input a filename following script !!"
        exit 1
}
[ ! -e "$1" ] &&{
        echo "ERROR:$1 is not exists !!"
        exit 2
}
for NAME in `cat $1`
do
        id $NAME &> /dev/null
        [ "$?" = "0" ] &&{
                echo $NAME is exists
        }||{
                useradd $NAME
                echo $NAME is created
        }
done

测试:

[root@shellexample mnt]# sh user_create.sh 
ERROR:Please input a filename following script !!
[root@shellexample mnt]# sh user_create.sh haha
ERROR:haha is not exists !!
[root@shellexample mnt]# sh user_create.sh userfile 
westos1 is created
westos2 is created
westos3 is created
[root@shellexample mnt]# sh user_create.sh userfile 
westos1 is exists
westos2 is exists
westos3 is exists

2.执行Host.sh后,会检测1-10号主机是否开启,如果开启显示主机名

3.while 语句结构

while 条件
do
done

4.while 语句示例
[root@shellexample mnt]# vim while.sh
#!/bin/bash
while true     ##当条件为真时
do
	read -p "Please input a word:" WORD
	[ "$WORD" = "exit" ] &&{
		echo bye
		exit
	}
	echo $WORD
done
[root@shellexample mnt]# sh while.sh 
Please input a word:haha
haha
Please input a word:yang
yang
Please input a word:exit
bye

脚本示例:
1.检查当前负载,并将定向数据一秒刷新一次

[root@shellexample mnt]# vim uptime.sh
#!/bin/bash
TTY=`ps $$ | grep $$ | awk '{print $2}'`

while true
do
        clear
        echo -n `uptime` > /dev/$TTY
        echo -ne '\r    \r'
        sleep 1
done
[root@shellexample mnt]# sh uptime.sh
02:50:55 up 6:45, 2 users, load average: 0.01, 0.04, 0.05

三、case语句

1.case 语句结构

case
        word1)
        action1
        ;;
        word2)
        action2
        ;;
        …
        *)
        action last
esac

2.case 语句示例

脚本示例:
1.执行action.sh后,输入a动作显示主机ip、输入b动作显示磁盘剩余空间、输入c动作显示系统运行时间、输入q动作退出脚本;

[root@shellexample mnt]# vim action.sh
#!/bin/bash
while true
do
        echo "
        A:显示主机ip
        B:显示磁盘剩余空间
        C:显示系统运行时间
        D:退出"
        read -p "Please input action:" Action

        case $Action in
                a|A)
                ifconfig eth0 | grep "inet " | awk '{print"当前主机ip:"$2}'
                ;;
                b|B)
                df -H | awk 'NR==2{print"剩余空间大小:"$4}'
                ;;
                c|C)
                uptime | awk '{print"系统运行了"$3""$4""}'
                ;;
                d|D)
                exit
                ;;
        esac
done

测试:

[root@shellexample mnt]# sh action.sh 

	A:显示主机ip
	B:显示磁盘剩余空间
	C:显示系统运行时间
	D:退出
Please input action:a
当前主机ip:172.25.254.173

	A:显示主机ip
	B:显示磁盘剩余空间
	C:显示系统运行时间
	D:退出
Please input action:b
剩余空间大小:7.5G

	A:显示主机ip
	B:显示磁盘剩余空间
	C:显示系统运行时间
	D:退出
Please input action:c
系统运行了24min,

	A:显示主机ip
	B:显示磁盘剩余空间
	C:显示系统运行时间
	D:退出
Please input action:d

四、expect 交互语句

1.expect 交互语句认知

expect 是自动应答命令用于交互式命令的自动执行
spawn 是expect 中的监控程序,其运行后会监控命令提出的交互问题

send 发送问题答案给交互命令
“\r” 表示回车
exp_continue 表示当问题不存在时继续回答下面的问题
expect eof 表示问题回答完毕退出 expect 环境
interact 表示问题回答完毕留在交互界面
set NAME[ lindex $argv n ] 定义变量

2.expect 示例
[root@shellexample mnt]# vim ask.sh
#!/bin/bash
read -p "what's your name:" NAME
read -p "How old are you:" AGE
read -p "Which class you study:" CLASS
read -p "You feel happy or terrible?" FEEL
echo $NAME is $AGE\'s old and study $CLASS feel $FEEL
[root@shellexample mnt]# chmod +x ask.sh
[root@shellexample mnt]# yum install expect -y
[root@shellexample mnt]# vim expect.sh
#!/bin/bash
/usr/bin/expect <<EOF
spawn /mnt/ask.sh
set timeout 3
expect {
"name:"         { send "$1\r" ; exp_continue }
"old"           { send "$2\r" ; exp_continue }
"class"         { send "$3\r" ; exp_continue }
"feel"          { send "$4\r" }
}
expect eof
EOF
[root@shellexample mnt]# sh expect.sh 1 2 3 4
spawn /mnt/ask.sh
what's your name:1
How old are you:2
Which class you study:3
You feel happy or terrible?4
1 is 2's old and study 3 feel 4

六、脚本中的语句控制器

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

[root@shellexample mnt]# vim test.sh
#!/bin/bash
for i in {1..10}
do
        while [ "$i" = "5" ]
        do
                echo luck num
        done
        echo $i
done
echo westos
[root@shellexample mnt]# sh test.sh
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num
luck num^C
[root@shellexample mnt]# vim test.sh
#!/bin/bash
for i in {1..10}
do
        while [ "$i" = "5" ]
        do
                echo luck num
                exit 55
        done
        echo $i
done
echo westos
[root@shellexample mnt]# sh test.sh
1
2
3
4
luck num
[root@shellexample mnt]# vim test.sh
#!/bin/bash
for i in {1..10}
do
        if
        [ "$i" = "5" ]
        then
                echo luck num
                break
        fi
        echo $i
done
echo westos
[root@shellexample mnt]# sh test.sh 
1
2
3
4
luck num
westos
[root@shellexample mnt]# vim test.sh
#!/bin/bash
for i in {1..10}
do
        if 
        [ "$i" = "5" ]
        then
                echo luck num
                continue
        fi
        echo $i
done
echo westos
[root@shellexample mnt]# sh test.sh 
1
2
3
4
luck num
6
7
8
9
10
westos

猜你喜欢

转载自blog.csdn.net/weixin_44209804/article/details/88314744