文章目录
1. for 循环
格式:
for 变量 in item1 item2 … itemN
do
语句1
语句2
done
写成一行:
for 变量 in item;do 语句1;语句2 done;
for语句定义变量四种形式
for var in $(seq 起点 终点 步长)
for var in 1 2 3 ... 10
for var in{
1..10}
for((var=0;var<10;var++))
#无限循环
for (( ; ; ))
练习1:
脚本:check_host.sh
检测10台主机与您当前主机直连主机是否通畅,如果通畅清显示主机ip列表
#!/bin/bash
for i in {
1..10}
do
ping -c1 -w1 172.25.254.$i &> /dev/null && {
echo " 172.25.254.$i tongchang !"
}
done
练习2:
脚本create.sh
- 自动创建用户
- 脚本后加 用户名文件、密码文件
- 自动检测用户文件的用户是否存在,不存在则自动创建,且密码为相对应的密码文件中的内容
#!/bin/bash
length=`sed -n '$=' user_file`
for LINE in `seq 1 $length`
do
uName=`sed -n ${
LINE}p $1`
uPass=`sed -n ${
LINE}p $2`
id $uName &> /dev/null && {
echo "$uName is exist !"
} || {
useradd $uName
echo $uPass | passwd --stdin $uName &> /dev/null
echo "$uName is created !"
}
done
2. while 循环(条件为真)
while 条件
do
语句1
语句2
done
无限循环 格式1:
while true
do
语句
done
无限循环 格式2:
while :
do
语句
done
3. until 循环(条件为假)
until 循环执行一系列命令直至条件为 true 时停止。
until 循环与 while 循环在处理方式上刚好相反。
一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用
until 条件
do
语句1
语句2
done
4. if
if 条件
then
语句
elif 条件
then
语句
else
语句
fi
(如果else分支没有语句要执行,就不要写这个else)
练习1:
check_file.sh
检测文件是否存在,并判断文件类型
#!/bin/bash
while true
do
read -p "Please input filename:" fileName
if [ "$fileName" == "exit" ]
then
exit
fi
if [ -e "$fileName" ]
then
if [ -f "$fileName" ]
then
echo "$fileName is 普通文件 ! "
elif [ -d "$fileName" ]
then
echo "$fileName is 目录 ! "
elif [ -S "$fileName" ]
then
echo "$fileName is 套接字 ! "
elif [ -L "$fileName" ]
then
echo "$fileName is 软链接 ! "
elif [ -b "$fileName" ]
then
echo "$fileName is 块设备 ! "
elif [ -c "$fileName" ]
then
echo "$fileName is 字符设备 ! "
fi
else
echo "$fileName is not exist !"
fi
done
5. case
case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令
case 值 in
模式1)
语句
;;
模式2)
语句
;;
*)
语句
;;
esac
("*)"表示其他情况)
练习:
system_watch.sh 后加disk或memory或upload,执行如下:
- disk监控磁盘使用情况
- memory监控内存使用情况
- upload监控启动负载
- 每秒显示
#!/bin/bash
while true
do
[ "$1" = "exit" ] && {
exit
}
case $1 in
disk|DISK)
watch -n 1 "df -h "
exit
;;
memory|MEMORY)
watch -n 1 "free -m"
exit
;;
upload|UPLOAD)
watch -n 1 "uptime"
exit
;;
esac
done
6. expect 自动会话
dnf install expect -y
:安装expect
问题脚本: ask.sh (给予执行权限)
#!/bin/bash
read -p "What's your name: " NAME
read -p "How old are you: " AGE
read -p "which class? " CLASS
read -p "How do you feel? " FEEL
echo $NAME is $AGE\'s old study $CLASS fell $FEEL
应答脚本1: answer.exp
#!/usr/bin/expect
spawn /mnt/ask.sh
expect "name"
send "tom\r"
expect "old"
send "18\r"
expect "class"
send "linux\r"
expect "feel"
send "happy\r"
expect eof
应答脚本2: answer.exp
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect {
"name" {
send "tom\r"; exp_continue }
"old" {
send "18\r"; exp_continue }
"class" {
send "linux\r" ; exp_continue }
"feel" {
send "happy\r" }
}
expect eof
应答脚本3: answer.exp
#!/usr/bin/expect
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set CLASS [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]
set timeout 2
spawn /mnt/ask.sh
expect {
"name" {
send "$NAME\r" ; exp_continue }
"old" {
send "$AGE\r" ; exp_continue }
"class" {
send "$CLASS\r" ; exp_continue }
"feel" {
send "$FEEL\r" }
}
expect eof
应答脚本4: answer.sh
#!/bin/bash
echo hello zy !
/usr/bin/expect <<EOF
set timeout 2
spawn /mnt/ask.sh
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
练习:
host_list.sh
检测172.25.254.1-172.25.254.10网络是否正常开启,如果网络正常 请生成解析列表host_list
#!/bin/bash
EXPECT()
{
/usr/bin/expect <<EOF
set timeout 2
spawn ssh root@$1 hostname
expect {
"yes/no?" { send "yes\r" ;
expect "password:" ;
send "$2\r";
exp_continue; }
"password:" { send "$2\r" }
}
expect eof
EOF
}
for i in {
1..10}
do
ping -c1 -w1 172.25.254.$i &> /dev/null
if [ "$?" = "0" ]
then
host_name=`EXPECT 172.25.254.$i westos | tail -n 1`
grep $host_name /mnt/host_list &> /dev/null || {
echo "172.25.254.$i $host_name" >> /mnt/host_list
}
fi
done
sed 's/^M//g' -i /mnt/host_list
#^M为<ctrl>+<V>+<M>
7. 跳出循环 (break 与 continue 与 exit)
break:允许跳出所有循环(终止执行后面的所有循环)
continue:它不会跳出所有循环,仅仅跳出当前循环
exit:退出