一、for语句
1.for 语句格式
for
do
done
为NUM 赋值1 2 3 的三种方法
for NUM in 1 2 3
for NUM in {1..3}
for NUM in `seq 1 3`
为NUM 赋值 从1开始每隔3赋给NUM,到10结束
for NUM in `seq 1 3 10` 1:起始值 3:步距 10:超过10终止
[root@host2 mnt]# vim for.sh
[root@host2 mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 3`
do
echo $NUM
sleep 1 执行脚本的时间间隔为1s
done
[root@host2 mnt]# sh for.sh
1
2
3
[root@host2 mnt]# vim for.sh
[root@host2 mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 3 10`
do
echo $NUM
sleep 1
done
[root@host2 mnt]# sh for.sh
1
4
7
10
[root@host2 mnt]# vim for.sh
[root@host2 mnt]# cat for.sh
#!/bin/bash
for NUM in `seq 1 3 12`
do
echo $NUM
sleep 1
done
[root@host2 mnt]# sh for.sh
1
4
7
10
检测本机是否能够ping通其他主机
[root@host2 mnt]# vim ping.sh
[root@host2 mnt]# cat ping.sh
#!/bin/bash
for NUM in `seq 1 10`
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up
done
[root@host2 mnt]# sh ping.sh
172.25.254.1 is up
172.25.254.2 is up
172.25.254.10 is up
创建用户
[root@host2 mnt]# vim useradd_ctrl.sh
[root@host2 mnt]# cat useradd_ctrl.sh
#!/bin/bash
MAX_LINE=`awk 'BEGIN{N}{N++}END{print N}' $1`
for Num in `seq 1 $MAX_LINE`
do
User_name=`sed -n ${Num}p $1`
passwd=`sed -n ${Num}P $2`
useradd $User_name
echo $passwd | passwd --stdin $User_name
done
[root@host2 mnt]# sh useradd_ctrl.sh userfile passwdfile
Creating mailbox file: File exists
Changing password for user mark.
passwd: all authentication tokens updated successfully.
Creating mailbox file: File exists
Changing password for user hat.
passwd: all authentication tokens updated successfully.
Creating mailbox file: File exists
Changing password for user jack.
passwd: all authentication tokens updated successfully.
[root@host2 mnt]# su - student
Last login: Wed May 16 19:39:52 CST 2018 on pts/0
[student@host2 ~]$ su - mark
Password:
Last login: Thu May 17 16:26:57 CST 2018 on pts/0
[mark@host2 ~]$ su - hat
Password:
[hat@host2 ~]$ su - jack
Password:
[jack@host2 ~]$
10秒倒计时
[root@host2 mnt]# vim time_end.sh
[root@host2 mnt]# cat time_end.sh
#!/bin/bash
for Sec in {10..1}
do
echo -n "after $Sec's is end"
echo -ne "\r \r"
sleep 1
done
二、While语句
1.编写脚本Use.sh,当根分区内存使用量超过80%时,每隔10s生成警告日志
[root@host2 mnt]# vim use_disk.sh
[root@host2 mnt]# cat use_disk.sh
#!/bin/bash
while true
do
Disk_Use=`df | awk -F " " '/\/$/{print $5}' | awk -F "%" '{print $1}'`
[ "$Disk_Use" -ge "50" ] && {
echo warning: disk will full!!
}
sleep 10
done
[root@host2 mnt]# sh use_disk.sh
warning: disk will full!!
warning: disk will full!!
^C
执行命令后不显示生成日志
[root@host2 mnt]# cat use_disk.sh
#!/bin/bash
while true
do
Disk_Use=`df | awk -F " " '/\/$/{print $5}' | awk -F "%" '{print $1}'`
[ "$Disk_Use" -ge "50" ] && {
echo warning: disk will full!! &>> /var/log/messages
}
sleep 10
done
[root@host2 mnt]# sh use_disk.sh
^C
[root@host2 mnt]# cat /var/log/messages
warning: disk will full!!
warning: disk will full!!
continue 继续执行该程序
break 立即停止再次执行
exit 退出
[root@host2 mnt]# cat finish1.sh
#!/bin/bash
for i in {1..10}
do
while [ "$i" = "5" ]
do
echo $i hello world
i=6
continue
done
echo $i
done
[root@host2 mnt]# sh finish1.sh
1
2
3
4
5 hello world
6
6
7
8
9
10
[root@host2 mnt]# sh -x finish1.sh
+ for i in '{1..10}'
+ '[' 1 = 5 ']'
+ echo 1
1
+ for i in '{1..10}'
+ '[' 2 = 5 ']'
+ echo 2
2
+ for i in '{1..10}'
+ '[' 3 = 5 ']'
+ echo 3
3
+ for i in '{1..10}'
+ '[' 4 = 5 ']'
+ echo 4
4
+ for i in '{1..10}'
+ '[' 5 = 5 ']'
+ echo 5 hello world
5 hello world
+ i=6
+ continue
+ '[' 6 = 5 ']'
+ echo 6
6
+ for i in '{1..10}'
+ '[' 6 = 5 ']'
+ echo 6
6
+ for i in '{1..10}'
+ '[' 7 = 5 ']'
+ echo 7
7
+ for i in '{1..10}'
+ '[' 8 = 5 ']'
+ echo 8
8
+ for i in '{1..10}'
+ '[' 9 = 5 ']'
+ echo 9
9
+ for i in '{1..10}'
+ '[' 10 = 5 ']'
+ echo 10
10
[root@host2 mnt]# cat finish1.sh
#!/bin/bash
for i in {1..10}
do
while [ "$i" = "5" ]
do
echo $i hello world
exit
done
echo $i
done
[root@host2 mnt]# sh finish1.sh
1
2
3
4
5 hello world
[root@host2 mnt]# sh -x finish1.sh
+ for i in '{1..10}'
+ '[' 1 = 5 ']'
+ echo 1
1
+ for i in '{1..10}'
+ '[' 2 = 5 ']'
+ echo 2
2
+ for i in '{1..10}'
+ '[' 3 = 5 ']'
+ echo 3
3
+ for i in '{1..10}'
+ '[' 4 = 5 ']'
+ echo 4
4
+ for i in '{1..10}'
+ '[' 5 = 5 ']'
+ echo 5 hello world
5 hello world
+ exit
[root@host2 mnt]# cat finish.sh
#!/bin/bash
for i in {1..10}
do
NUM=$[ $i%2 ]
while [ "$NUM" = "0" ]
do
echo $i
break
done
done
[root@host2 mnt]# sh finish.sh
2
4
6
8
10
[root@host2 mnt]# sh -x finish.sh
+ for i in '{1..10}'
+ NUM=1
+ '[' 1 = 0 ']'
+ for i in '{1..10}'
+ NUM=0
+ '[' 0 = 0 ']'
+ echo 2
2
+ break
+ for i in '{1..10}'
+ NUM=1
+ '[' 1 = 0 ']'
+ for i in '{1..10}'
+ NUM=0
+ '[' 0 = 0 ']'
+ echo 4
4
+ break
+ for i in '{1..10}'
+ NUM=1
+ '[' 1 = 0 ']'
+ for i in '{1..10}'
+ NUM=0
+ '[' 0 = 0 ']'
+ echo 6
6
+ break
+ for i in '{1..10}'
+ NUM=1
+ '[' 1 = 0 ']'
+ for i in '{1..10}'
+ NUM=0
+ '[' 0 = 0 ']'
+ echo 8
8
+ break
+ for i in '{1..10}'
+ NUM=1
+ '[' 1 = 0 ']'
+ for i in '{1..10}'
+ NUM=0
+ '[' 0 = 0 ']'
+ echo 10
10
+ break
三、if 语句
1.创建用户
[root@host2 mnt]# vim useradd_ctrl1.sh
[root@host2 mnt]# cat useradd_ctrl1.sh
#!/bin/bash
if [ "$#" -lt "2" ]
then
echo Error:please input a userfile and passwdfile after scripts!!
elif [ ! -e "$1" ]
then
echo Error:$1 is not exist!!
elif [ ! -e "$2" ]
then
echo Error:$2 is not exist!!
else
MAX_LINE=`awk 'BEGIN{N}{N++}END{print N}' $1`
for Num in `seq 1 $MAX_LINE`
do
User_name=`sed -n ${Num}p $1`
passwd=`sed -n ${Num}P $2`
useradd $User_name
echo $passwd | passwd --stdin $User_name
done
fi
[root@host2 mnt]# sh useradd_ctrl1.sh userfile passwdfile
Creating mailbox file: File exists
Changing password for user mark.
passwd: all authentication tokens updated successfully.
Creating mailbox file: File exists
Changing password for user hat.
passwd: all authentication tokens updated successfully.
Creating mailbox file: File exists
Changing password for user jack.
passwd: all authentication tokens updated successfully.
四、case语句
case语句横向同时比较,效率优于if语句
1.编写脚本test1.sh,当输入cat时,输出dog;输入dog时,输出cat;其他则报错
[root@host2 mnt]# vim test1.sh
[root@host2 mnt]# cat test1.sh
#!/bin/bash
case "$1" in
cat)
echo dog
;;
dog)
echo cat
;;
*)
echo error
esac
[root@host2 mnt]# sh test1.sh cat
dog
[root@host2 mnt]# sh test1.sh dog
cat
[root@host2 mnt]# sh test1.sh sheep
error
2.编写脚本service_ctrl.sh,当输入命令时,对httpd服务做不同操作
[root@host2 mnt]# cat service_ctrl.sh
#!/bin/bash
case $1 in
start)
systemctl start httpd
;;
port)
netstat -antuple | grep httpd
;;
chport)
sed -e '/^Listen/s/Listen\ 80' -i /etc/httpd/conf/httpd.conf
;;
stop)
systemctl stop httpd
;;
status)
systemctl status httpd
;;
*)
echo Error:$1 is not found!!
esac
[root@host2 mnt]# sh service_ctrl.sh start
[root@host2 mnt]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2018-05-17 20:18:40 CST; 7s ago
[root@host2 mnt]# sh service_ctrl.sh port
tcp6 0 0 :::80 :::* LISTEN 0 71326 30200/httpd
[root@host2 mnt]# sh service_ctrl.sh chport
[root@host2 mnt]# sh service_ctrl.sh port
tcp6 0 0 :::8080 :::* LISTEN 0 71326 30200/httpd
[root@host2 mnt]# sh service_ctrl.sh stop
[root@host2 mnt]# sh service_ctrl.sh status
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
五、expect语句
注意:需要安装expect服务
expect:自动应答脚本,具备自己的运行环境;针对某一脚本,自动输出结果
1.编写脚本passwd.exp,自动修改root用户密码查看相关命令的脚本:
查看相关命令的脚本:
[root@localhost mnt]# which passwd
/bin/passwd
[root@localhost mnt]# which expect
/bin/expect
[root@host2 mnt]# vim passwd.exp
[root@host2 mnt]# cat passwd.exp
#!/usr/bin/expect
spawn /bin/passwd
expect "New"
send "redhat\r"
expect "Retype"
send "redhat\r"
expect eof
[root@host2 mnt]# expect passwd.exp
spawn /bin/passwd
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.
2.自动修改普通或root用户密码
[root@host2 mnt]# vim passwd2.exp
[root@host2 mnt]# cat passwd2.exp
#!/usr/bin/expect
set USER [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn passwd $USER
expect "New"
send "$PASS\r"
expect "Retype"
send "$PASS\r"
expect eof
[root@host2 mnt]# expect passwd2.exp mark student
spawn passwd mark
Changing password for user mark.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@host2 mnt]# expect passwd2.exp root redhat
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.
3.编写脚本ssh.exp,自动链接指定主机
[root@host2 mnt]# vim ssh.exp
[root@host2 mnt]# cat ssh.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
set PASSWD [ lindex $argv 1 ]
spawn ssh root@$IP
expect {
"yes/no" { send "yes\r"; exp_continue }
"password" { send "$PASSWD\r" }
}
interact
#expect eof
[root@host2 mnt]#
[root@host2 mnt]# expect ssh.exp 172.25.254.1 redhat
spawn ssh root@172.25.254.1
The authenticity of host '172.25.254.1 (172.25.254.1)' can't be established.
ECDSA key fingerprint is SHA256:pVWxMALdcMk9ONSeH+dyGQ2OSNqVp4qwrwfd78EbhXY.
ECDSA key fingerprint is MD5:ff:4e:a2:e9:94:61:db:d5:ae:26:17:67:34:d9:94:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.1' (ECDSA) to the list of known hosts.
root@172.25.254.1's password:
Last login: Thu May 17 09:35:31 2018 from 172.25.254.10
[root@host1 ~]# logout
Connection to 172.25.254.1 closed.
六、倒计时
编写脚本1:10倒计时
[root@host2 mnt]# vim time_end1.sh
[root@host2 mnt]# cat time_end1.sh
#!/bin/bash
MIN=1
SEC=10
for ((SEC=$SEC;SEC>=0;SEC--))
do
[ "$SEC" -eq "0" -a "$MIN" -eq "0" ]&& exit 0
while [ "$SEC" -eq "0" -a "$MIN" -ge "0" ]
do
echo -n "After $MIN:$SEC is end "
sleep 1
((MIN--))
SEC=59
echo -ne "\r \r"
done
echo -n "After $MIN:$SEC is end "
sleep 1
echo -ne "\r \r"
done
[root@host2 mnt]# sh time_end1.sh
After 1:5 is end^C
[root@host2 mnt]# sh time_end1.sh
After 0:57 is end^C