脚本见shell脚本练习分类专栏
1.for ##循环
for 定义变量
do 使用变量,执行动作
done 结束标志
格式1:
#!/bin/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列表
for ip in {1..10}
do
ping -w1 -c1 172.25.254.$ip &>/dev/null && {
echo 172.25.254.$ip
}
done
脚本练习 倒计时
==1==
for i in {10..1}
at do
clear ##不输出
echo -n $i
sleep 1
done
==2==
for i in {10..1}
do
clear
echo -n ${i}
echo -ne "\r \r"
sleep 1
done
2.while
while ture #条件为真
do #条件成立所作循环动作
done
3.until
until false ##条件为假
do$(date | cut -d " " -f 4 | cut -d : -f 3
#条件不成立所作循环动作
done
4.if
if
then
elif
then
...
else
fi
脚本练习:
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
while true
do
read -p " please input filename : " filename
if [ "$filename" = "exit" -o "$filename" = "EXIT" ]
then
echo bye
exit
elif [ -z "$filename" ]
then
echo -n ""
elif [ ! -e "$filename" ]
then
echo $filename is not exist
elif [ -f "$filename" ]
then
echo $filename is file
elif [ -d "$filename" ]
then
echo $filenmae is direcory
else
echo ukown file type
fi
done
5.case
功能开关
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
脚本练习
user_ctrl.sh
[ "$USER" = "root" ]||{
echo please run $0 with super user
exit
}
while true
do
read -p "please input action [add|del|exit]:" action
case $action in
add|ADD|Add )
read -p " please input username:" username
id $username &> /dev/null
if [ "$?" = "0" ]
then
echo $username is exist
else
useradd $username &>/dev/null
read -p "please input password :" password
echo $password | passwd --stdin $username
echo passwd is created
fi
;;
del|DEL|Del)
read -p "please input username:" username
id $username &>/dev/null
if [ "$?" = "0" ]
then
userdel -r $username
echo $username is deleted
else
echo $username is not exist
fi
;;
exit)
echo bye
exit
;;
esac
done
脚本练习
system_watch.sh disk memory upload (每秒显示)
disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载
case $1 in
disk)
watch -t -n 1 df -h
;;
memory)
watch -t -n 1 free -m
;;
upload)
watch -t -n 1 uptime
;;
*)
echo error: unkown error
esac
6.expect
问题脚本
#!/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
应答脚本
#!/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 /mnt/ask.sh
expect {
"name" { send "$NAME\r";exp_continue }
"old" { send "$AGE\r";exp_continue }
"objective" { send "$OBJ\r";exp_continue }
"ok" { send "$FEEL\r" }
}
expect eof
脚本练习
查看当前主机能连接的主机的主机名
[root@localhost mnt]# cat host_list.sh
#!/bin/bash
EXPECT(){
/usr/bin/expect << EOF
set timeout 3
spawn $1
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for IP in {1..10}
do
ping -c1 -w1 172.25.254.$IP &>/dev/null &&{
EXPECT "ssh root@172.25.254.$IP hostname" | tail -n 1
}
done
脚本练习
查看能连接的主机里面有没有westos用户,如果没有,则建立westos用户,密码westos
[root@localhost mnt]# cat host_list.sh
#!/bin/bash
EXPECT(){
/usr/bin/expect << EOF
set timeout 3
spawn ssh root@172.25.254.$1 "$2"
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for IP in {1..10}
do
ping -c1 -w1 172.25.254.$IP &>/dev/null &&{
USER_STATE=`EXPECT $IP " getent passwd $1" | grep -E '/authenticity|ECDSA|connecting|Warning|sqawn|password' -v `
if [ -n "$USER_STATE" ]
s then
echo "$1 on 172.25.254.$IP is exist "
else
EXPECT $IP "useradd $1 && echo $2 | passwd --stdin $1 "
fi
}
done
sh host_list.sh westos westos
7.break,continue,exit
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出