1.for ##循环
for 定义变量
do 使用变量,执行动作
done 结束标志
格式1:
#!/bin/env bash
for WESTOS in `seq 2 2 10`
do
echo $WESTOS
done
格式2:
for WESTOS in 1 2 3
do
echo $WESTOS
done
格式3:
for WESTOS in {10…1}
do
echo $WESTOS
done
格式4:
for ((WESTOS=0;WESTOS<10;WESTOS++))
do
echo $WESTOS
done
脚本练习:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表
[root@node1 mnt]# vim check_host.sh
#!/bin/bash
for i in {1..10}
do
ping -c1 -w1 172.25.254.$i &> /dev/null && {
echo 172.25.254.$i
}
done
[root@server3 ~]# vim createuser.sh
[root@server3 ~]# sh createuser.sh userfile passfile
user1 is created!!
user2 is created!!
user3 is created!!
[root@server3 ~]# sh createuser.sh userfile passfile
user1 is exist !!
user2 is exist !!
user3 is exist !!
[root@server3 ~]# cat createuser.sh
#!/bin/env bash
Line_Num=`sed -n '$=' userfile`
for LINE in `seq 1 $Line_Num `
do
USERNAME=`sed -n ${LINE}p $1`
PASSWORD=`sed -n ${LINE}p $2`
id $USERNAME &> /dev/null && {
echo $USERNAME is exist !!
}||{
useradd $USERNAME
echo $PASSWORD |passwd --stdin $USERNAME &> /dev/null && echo $USERNAME is created!!
}
done
2.while
while true #条件为真
do #条件成立所作循环动作
read -p "Please input word: " WORD
echo $WORD
done
3.until
until false ##条件为假
do #条件不成立所作循环动作
read -p "Please input word: " WORD
echo $WORD
done
4.if
if
then
elif
then
…
else
fi
[root@server3 ~]# vim file.sh
#!/bin/env bash
if [ -e "/mnt/file" ] ##当前面的条件符合 后面的语句就不执行
then
echo file
elif [ -e "/mnt/file1" ]
then
echo file1
else
echo no
fi
[root@server3 ~]# touch /mnt/file
[root@server3 ~]# sh file.sh
file
[root@server3 ~]# touch /mnt/file1
[root@server3 ~]# sh file.sh
file
脚本练习:
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
5.case
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
脚本练习
system_watch.sh disk memory upload (每秒显示)
disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载
[root@node1 mnt]# vim system_watch.sh
[root@node1 mnt]# sh system_watch.sh disk
[root@node1 mnt]# sh system_watch.sh memory
[root@node1 mnt]# sh system_watch.sh upload
6.expect
问题脚本
[root@server3 ~]# vim ask.sh
#!/bin/bash
read -p "what’s your name:" NAME
read -p "How old are you: " AGE
read -p "Which objective: " OBJ
read -p "Are you ok? " OK
echo $NAME is $AGE’s old study $OBJ feel $OK
[root@server3 ~]# chmod +x ask.sh
[root@server3 ~]# sh ask.sh
what’s your name:tom
How old are you: 18
Which objective: linux
Are you happy? happy
tom is 18’s old study linux feel happy
[root@server3 ~]# dnf install -y expect
[root@server3 ~]# vim answer.exp
应答脚本
#!/usr/bin/expect
spawn /root/ask.sh
expect "name"
send "tom\r"
expect "old"
send "18\r"
expect "objective"
send "linux\r"
expect "happy"
send "happy\r"
expect eof
[root@server3 ~]# expect answer.exp
spawn /root/ask.sh
what’s your name:tom
How old are you: 18
Which objective: linux
Are you happy? happy
tom is 18’s old study linux feel happy
[root@server3 ~]# sh ask.sh << EOF
> tom
> 18
> linux
> happy
> EOF
tom is 18’s old study linux feel happy
[root@server3 ~]# vim ask.sh ##注释age一行
[root@server3 ~]# sh ask.sh << EOF
tom
18
linux
happy
EOF
tom is ’s old study 18 feel linux
[root@server3 ~]# expect answer.exp
spawn /root/ask.sh
what’s your name:tom
Which objective: 18
linux
Are you happy? tom is ’s old study 18 feel linux
happy
##当缺少某个问题时,多个expect回答问题会造成答案错乱,脚本优化
[root@server3 ~]# vim answer.exp
#!/usr/bin/expect
set timeout 1
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set OBJ [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]
spawn /root/ask.sh
expect {
"name" { send "$NAME\r";exp_continue }
"old" { send "$AGE\r";exp_continue }
"objective" { send "$OBJ\r";exp_continue }
"happy" { send "$FEEL\r" }
}
expect eof
[root@server3 ~]# expect answer1.exp lee 19 c++ happy
spawn /root/ask.sh
what’s your name:lee
How old are you: 19
Which objective: c++
Are you happy? happy
lee is ’s old study c++ feel happy
[root@server3 ~]# vim answer.sh
#!/bin/env bash
echo hello westos
/usr/bin/expect <<EOF
spawn /root/ask.sh
expect {
"name" { send "$1\r";exp_continue }
"old" { send "$2\r";exp_continue }
"objective" { send "$3\r";exp_continue }
"happy" { send "$4\r" }
}
expect eof
EOF
[root@server3 ~]# sh answer.sh lee 19 linux bad
hello westos
spawn /root/ask.sh
what’s your name:lee
Which objective: linux
Are you happy? bad
lee is ’s old study linux feel bad
脚本练习
auto_ssh 192.168.0.1 westos
可以自动连接目标主机当目标主机网络不通时报错
[ “$?” = “0”] 退出值=0 表示命令执行正确 不等于0 表示ping不同
[root@server3 ~]# asd
-bash: asd: command not found
[root@server3 ~]# echo $?
127
[root@server3 ~]# ls
[root@server3 ~]# echo $?
0
[root@server3 ~]# cat test.sh
#!/bin/env bash
asdffsad
[root@server3 ~]# sh test.sh
test.sh: line 2: asdffsad: command not found
[root@server3 ~]# echo $?
127
[root@server3 ~]# vim test.sh
[root@server3 ~]# cat test.sh ##更改退出值
#!/bin/env bash
asdffsad
exit 0
[root@server3 ~]# sh test.sh
test.sh: line 2: asdffsad: command not found
[root@server3 ~]# echo $?
0
7.break,continue,exit
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出
continue
break
exit