一、for语句
1.for语句格式
for i in {1..10} | seq 1 3 10
(步距为3,即每个3执行命令)
do
echo $i
sleep 1 ##执行命令后休眠时间为1s
[root@localhost mnt]# vim test.sh
1 #!/bin/bash
2 for i in `seq 1 3 10`
3 do
4 echo $i
5 sleep 1
6 done
1-10 步距为3
2.编写脚本check_host检查1-10的主机是否可达
[root@localhost mnt]# cat check_host.sh
!/bin/bash
for IP in {1..10}
do
ping -c1 -w1 172.25.254.IP &> /dev/null && echo 172.25.254.
IP &> /dev/null && echo 172.25.254.
IP is up
done
1 #!/bin/bash
2 for IP in {1..10}
3 do
4 ping -c1 -w1 172.25.254.$IP &>/dev/null && echo 172.25.254.$IP is up
5 done
[root@localhost mnt]# sh test.sh
172.25.254.2 is up
3.编写脚本create_user自动建立userfile的用户
[root@localhost mnt]# cat create_user.sh
!/bin/bash
for i in cat $1
do
useradd $i
done
4.编写脚本将/mnt/userfile与/mnt/passwdfile一一对应,建立
1 #!/bin/bash
2 n=awk 'BEGIN{N=0}{N++}END{print N}' /mnt/userfile
3 for Num in seq 1 "$n"
4 do
5 User_name=sed -n ""$Num"p" /mnt/userfile
6 Passwd=sed -n ""$Num"p" /mnt/passwdfile
7 useradd
Username8echo
U
s
e
r
n
a
m
e
8
e
c
h
o
Passwd | passwd --stdin $User_name
9 done
编写10s 倒计时
[root@localhost mnt]# vim time_test.sh
1 #!/bin/bash
2 for Time in {10..1}
3 do
4 echo -n "after $Time's is end"
5 echo -ne "\r \r"
6 sleep 1
7 done
[root@localhost mnt]# sh time_test.sh
after 8's is endd
[root@localhost mnt]# vim user_test.sh
1 #!/bin/bash
2 n=awk 'BEGIN{N=0}{N++}END{print N}' $1
3 for Num in seq 1 $n
4 do
5 User_name=sed -n ${Num}p $1
6 Passwd=sed -n ${Num}p $2
7 useradd
Username8echo
U
s
e
r
n
a
m
e
8
e
c
h
o
Passwd | passwd --stdin $User_name
9 done
[root@localhost mnt]# sh user_test.sh file1 file2
[root@localhost mnt]# sh user_test.sh file1 file2
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
二、while语句
1.编写脚本Use.sh,当根分区内存使用量超过80%时,每隔10s生成警告日志
1 #!/bin/bash
2 #Use_Memory=`df | awk -F " " '/\/$/{print $5}'`
3 Use_Memory=`df | awk -F " " '/\/$/{print $5}' | cut -d "%" -f 1`
4 while [ $Use_Memory -gt "80" ]
5 do
6 echo "Permissive: Ues_Memory of root directory overflows 80%" >> /var/log/messages
7 sleep 10
8 done
或者
1 #!/bin/bash
2 while true
3 do
4 Use_Memory=`df | awk -F " " '/\/$/{print $5}' | cut -d "%" -f 1`
5 [ $Use_Memory -gt "80" ] && {
6 echo "Permissive: Ues_Memory of root directory overflows 80% " >> /var/log/messages
7 }
8 sleep 10
9 done
[root@localhost mnt]# vim use.sh
1 #!/bin/bash
2 while true
3 do
4 Use_memory=df | awk -F " " '/\/$/{print $5}' | awk -F "%" '{print $ 1}'
5 [ “$Use_memory” -ge “5” ] && {
6 echo wornin:full >> /var/log/messages
7 }
8 sleep 3
9 done
~
三、if语句
1.编写脚本User_create.sh,当文件个数不符合要求或者文件不存在时报错
复杂型:
1 #!/bin/bash
2 if
3 [ $# -eq 2 ]
4 then
5 [ -e $1 ] && {
6 [ -e $2 ] && {
7 n=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
8 for Num in `seq 1 "$n"`
9 do
10 User_name=`sed -n ""$Num"p" $1`
11 Passwd=`sed -n ""$Num"p" $2`
12 useradd $User_name
13 echo $Passwd | passwd --stdin $User_name
14 done }
15 } || echo Error!!
16 else
17 echo Error!!
18 fi
简单型
19 if [ $# -lt 2]
20 then
21 echo Error:please input a user file and a passwd file !!
22 elif [ ! -e $1 ]
23 then
24 echo Error:$1 is not exist!!
25 elif [ ! -e $2 ]
26 then
27 echo Error:$2 is not exist!!
28 else
29 n=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
30 for Num in `seq 1 "$n"`
31 do
32 User_name=`sed -n ""$Num"p" $1`
33 Passwd=`sed -n ""$Num"p" $2`
34 useradd $User_name
35 echo $Passwd | passwd --stdin $User_name
36 done
37 fi
[root@localhost mnt]# vim if_test.sh
1 #!/bin/bash
2 if [ “#” -lt “2” ]
3 then
4 echo ERROR:please input a userfile and password file after scripts ! !
5 elif [ ! -e “
#” -lt “2” ] 3 then 4 echo ERROR:please input a userfile and password file after scripts ! ! 5 elif [ ! -e “
1” ]
6 then
7 echo ERROR:
1isnotexist!!8elif[!−e“
1
i
s
n
o
t
e
x
i
s
t
!
!
8
e
l
i
f
[
!
−
e
“
2” ]
9 then
10 echo ERROR: $2 is not exist!!
11 else
12 M=awk 'BEGIN{N=0}{N++}END{print N}' $1
13 for Num in seq 1 $M
14 do
15 User_name=sed -n ${Num}p $1
16 passwd=sed -n ${Num}p $2
17 useradd
Username18echo
U
s
e
r
n
a
m
e
18
e
c
h
o
passwd | passwd --stdin $User_name
19 done
20 fi
[root@localhost mnt]# sh if_test.sh file1 file2
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
2.编写脚本test.sh,当输入cat时,输出dog;输入dog时,输出cat;其他则报错
1 #!/bin/bash
2 if [ "$1" = cat ]
3 then
4 echo dog
5 elif [ "$1" = dog ]
6 then
7 echo cat
8 else
9 echo Error:please input cat or dog after script!
10 fi
[root@localhost mnt]# vim test.sh
1 #!/bin/bash
2 if [ “
1”=cat]3then4echodog5elif[“
1
”
=
c
a
t
]
3
t
h
e
n
4
e
c
h
o
d
o
g
5
e
l
i
f
[
“
1” = dog ]
6 then
7 echo cat
8 else
9 echo ERROR:please input cat or dog after script!
10 fi
[root@localhost mnt]# sh test.sh cat
dog
四、case语句
case语句横向同时比较,效率优于if语句
1.编写脚本service_ctrl.sh,当输入命令时,对httpd服务做不同操作
1 #!/bin/bash
2 case $1 in
3 start)
4 systemctl start httpd
5 ;;
6 port)
7 netstat -antuple | grep httpd
8 ;;
9 stop)
10 systemctl stop httpd
11 ;;
12 *)
13 echo Error:$1 is not found!
14 esac
[root@localhost mnt]# vim service_ctrl.sh
1 #!/bin/bash
2 case
1in3start)4systemctlstarthttpd5;;6port)7netstat−antlupe|grephttpd8;;9stop)10systemctlstophttpd11;;12∗)13echoERROR:
1
i
n
3
s
t
a
r
t
)
4
s
y
s
t
e
m
c
t
l
s
t
a
r
t
h
t
t
p
d
5
;
;
6
p
o
r
t
)
7
n
e
t
s
t
a
t
−
a
n
t
l
u
p
e
|
g
r
e
p
h
t
t
p
d
8
;
;
9
s
t
o
p
)
10
s
y
s
t
e
m
c
t
l
s
t
o
p
h
t
t
p
d
11
;
;
12
∗
)
13
e
c
h
o
E
R
R
O
R
:
1 is not found
14 esac
[root@localhost mnt]# sh service_ctrl.sh port
tcp6 0 0 :::443 :::* LISTEN 0 75154 10333/httpd
tcp6 0 0 :::80 :::* LISTEN 0 75140 10333/httpd
五、expect语句
####注意:需要安装expect服务#####
expect:自动应答脚本,具备自己的运行环境;针对某一脚本,自动输出结果
1.编写脚本passwd.exp,自动修改root用户密码
查看相关命令的脚本:
[root@localhost mnt]# which passwd
/bin/passwd
[root@localhost mnt]# which expect
/bin/expect
编写脚本:
[root@localhost mnt]# cat passwd.exp
!/bin/expect
spawn /bin/passwd ##监控脚本
expect "New"
send "123\r"
expect "Retype"
send "123\r"
expect eof
修改密码
2.编写脚本ssh.exp,自动链接指定主机
1 #!/bin/expect
2 set IP [ lindex $argv 0 ]
3 set PASSWD [ lindex $argv 1 ]
4 spawn ssh root@$IP
5 expect {
6 "yes/no" { send "yes\r"; exp_continue}
7 "password" { send "$PASSWD\r" }
8 }
9 interact
10 #expect eof
[root@localhost mnt]# expect passwd.exp
spawn passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# vim passwd.exp
1 #!/bin/expect
2 spawn passwd root
3 expect "New"
4 send "123\r"
5 expect "Retype"
6 send "123\r"
7 expect eof
exp_continue 表示当问题不存在的时候继续回答下面的问题
expect eof 表示问题回答完毕退出
实验链接主机:
[root@localhost mnt]# expect ssh.exp 172.25.254.10 westos
spawn ssh root@172.25.254.10
root@172.25.254.10’s password:
Last login: Wed May 16 14:36:07 2018 from 172.25.254.200
[root@localhost mnt]# vim ssh.exp
1 #!/usr/bin/expect
2 set IP [ lindex $argv 0 ]
3 set pass [ lindex $argv 1 ]
4 spawn ssh root@$IP
5 expect {
6 "yes/no" { send yes\r; exp_continue }
7 "password" { send "$pass\r" }
8 }
9 interact
3.编写脚本check_host.sh,输出1-10网络通的主机名
[root@localhost mnt]# vim ping.sh
1 #!/bin/bash
2 for Num in {10..20}
3 do ping -c1 -w1 172.25.254.$Num &> /dev/null &&{
4 /usr/bin/expect <<EOF
5 set timeout 2
6 spawn ssh root@172.25.254.$Num `hostname`
7 expect {
8 "yes/no" { send "yes\r";exp_continue }
9 "password" { send "redhat\r" }
10 }
11 expect eof
12 EOF
13 }
14 done
[root@localhost mnt]# sh ping.sh
spawn ssh root@172.25.254.15 rng
六、脚本语句控制器
exit
break
continue
运算方式及运算符号
[root@localhost mnt]# echo $[ 1+2 ]
3
[root@localhost mnt]# echo $[ 5**2 ]
25
[root@localhost mnt]# echo $[ 5**3 ]
125
[root@localhost mnt]# echo $[ 5%3 ]
2
[root@localhost mnt]# echo $[ 5/3 ]
1
[root@localhost mnt]# vim test.sh
1 #!/bin/bash
2 for ((i=1;i<10;i++))
3 do
4 echo $i
5 done
[root@localhost mnt]# sh test.sh
1
2
3
4
5
6
7
8
9
n+=i 表示n+i=n
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# let c=$a+$b
[root@localhost mnt]# echo $c
2
七、倒计时
编写脚本1:10倒计时
[root@localhost mnt]# vim test.sh
1 #!/bin/bash
2 MIN=1
3 SEC=10
4 for ((SEC=$SEC;SEC>=0;SEC--))
5 do
6 [ "$SEC" -eq "0" -a "$MIN" -eq "0" ]&& exit 0
7 while [ "$SEC" -eq "0" -a "$MIN" -ge "0" ]
8 do
9 echo -n "After $MIN:$SEC is end "
10 sleep 1
11 ((MIN--))
12 SEC=59
13 echo -ne "\r \r"
14 done
15 echo -n "After $MIN:$SEC is end"
16 sleep 1
17 echo -ne "\r \r"
18 done