1.for语句
for 定义变量
do 使用变量,执行动作
done 结束标志
格式1:
#!bin/bash
for WESTOS in $(seq 1 2 10)
do
echo $WESTOS
done
格式2:
for WESTOS in westos linux lee
do
echo $WESTOS
done
格式3:
for WESTOS in {10..1}
do
echo $WESTOS
done
格式4:
for ((WESTOS=0;WESTOS=0;WESTOS<10;WESTOS++))
do
echo $WESTOS
done
脚本练习1:
check_host.sh
用此脚本检测当前10台主机与您当前主机直连主机是否网络通畅
如果通畅显示ip
#!/bin/bash
for ((IP=1;IP<11;IP++))
do
ping -c1 -w1 172.25.254.$IP &>/dev/null && echo 172.25.254.$IP
done
脚本练习2:
create_user.sh userfile passfile
#!/bin/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 #条件成立所循环的动作
done
3.until
until false #条件为假
do
#条件不成立所作循环动作
done
4.if
if
then
elif
#!/bin/bash
until false
do
read -p "please input word: " WORD
if [ "$WORD" = "exit" ]
then
echo bye
exit
elif [ ! -e "$WORD" ]
then
echo not exited
elif [ -d "$WORD" ]
then
echo is a dir
elif [ -f "$WORD" ]
then
echo is a file
fi
done
5.case
case $1 in
word1|WORD1)
action1
;;
word2|WORD2
action2
;;
*)
action3
esac
#!/bin/bash
read -p "please choose disk/memory/upload: " WORD
case $WORD in
disk)
watch -n 1 "df -h"
;;
memory)
watch -n 1 "free -h"
;;
upload)
top
;;
*)
echo "error words
esac
6. except
6.1问题脚本
vim 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 "Are you appy: " FEEL
echo $NAME is $AGE\'s old study $CLASS feel $FEEL
6.2应答脚本
vim answer.exp
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect "name"
send "lee\r"
expect "old"
send "18\r"
expect "class"
send "linux\r"
expect "happy"
send "happy\r"
expect eof
chmod +x ask.sh
此处用EOF导向ask.sh也可以实现:
在expect应答脚本中加入非交互输入
#!/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 1
spawn /mnt/ask.sh
expect {
"name" { send "$NAME\r" ; exp_continue }
"old" { send "$AGE\r" ; exp_continue }
"class" { send "$CLASS\r" ; exp_continue }
"happy" { send "$FEEL\r" }
}
expect eof
在shell中加入expect:
#!/bin/bash
echo hello westos
/usr/bin/expect << EOF
spawn /mnt/ask.sh
expect {
"name" { send "$1\r" ; exp_continue }
"old" { send "$2\r" ; exp_continue }
"class" { send "$3\r" ; exp_continue}
"happy" { send "$4\r" }
}
expect eof
EOF
练习脚本:
#!/bin/bash
AUTO_SSH()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
"yes/no" { send "yes\r" ; exp_continue }
"password:" { send "$2\r" }
}
expect eof
EOF
}
for Host_Num in {1..10}
do
ping -c1 -w1 172.25.254.$Host_Num &> /dev/null
if [ "$?" = "0" ]
then
Host_Name=`AUTO_SSH 172.25.254.$Host_Num westos | tail -n 1`
grep $Host_Name /mnt/hosts_list &> /dev/null || {
echo "172.25.254.$Host_Num $Host_Name" >> /mnt/hosts_list
}
fi
done
sed 's/^M//g' -i /mnt/hosts_list
7.break,continue,exit
continue | 终止当前循环直接进入下个循环 |
---|---|
break | 终止当前所在语句所有动作进行语句外的其他动作 |
exit | 脚本退出 |