常用的控制语句
- for语句
- while 和unitl 语句
- if 语句
- case 语句
- expect 语句
- exit , break , continu
一、For语句
for NUM in 1 2 3 循环
for NUM in {1..3}
for NUM in `seq 1 3` 或者 for NUM in `seq 1 2 10`
书写规范:写得时候先写完格式,在进行内容的编写
for条件
do
循环内容
done
例1:脚本输出倒计时
for time in {10..1}
do
echo -n "After ${time}s is end"
echo -ne "\r"
sleep 1
done
例2:找到可以pig通的主机的IP,并且将IP地址放到file文件里
[ -e "/mnt/file" ]&&{
echo /mnt/file is exit!!
echo "[O]overwrite [B]backup [S]skip"
read -p "what do you action: " WORD
ACTION=`echo $WORD| tr 'a-z' 'A-Z'`
[ "$ACTION" = "O" ] && {
rm -rf /mnt/file
}
[ "$ACTION" = "B" ] && {
mv /mnt/file /mnt/file.bak
}
[ "$ACTION" = "S" ] && {
exit 0
}
}
for ip in {1..20}
do
ping -c1 -w1 172.25.254.$ip &> /dev/null && {
echo 172.25.254.$ip >> /mnt/file
}
done
二、while和until
while 语句
while 条件
do
done
例:倒计时,循环方法
三、if语句
基本格式:
if
then
elif
then
。。。
else
fi
例1:判断文件类型
具体要求:输入文件或者目录,然后判断类型(判断普通文件,软链接,目录),直到输入exit后再退出判断,否则一直进行判断。
注:软链接文件生曾成:ln -s soucefile soft_link_file 源文件必须是绝对路径,生成的软链接文件任意
方法一:循环语句
while true
do
read -p "Please input file or exit: " file
if [ "$file" = "exit" -o "$file" = "EXIT" ]
then
echo bye
exit 0
elif [ -f "$file" ]
then
echo "$file is a common file!!"
elif [ -d "$file" ]
then
echo "$file is a directory!!"
elif [ -L "$file" ]
then
echo "$file is a softlink file"
else
echo "$file is not exist!!"
fi
done
方法二:函数法
ECHO() 输出字体颜色的函数
{
echo -e "\033[${1}m$2\033[0m"
}
FILE()
{
read -p "Please input file or exit: " file
if [ "$file" = "exit" -o "$file" = "EXIT" ]
then
ECHO 032 Bye
exit 0
elif [ -f "$file" ]
then
ECHO 031 "$file is a common file!!"
elif [ -d "$file" ]
then
ECHO 031 "$file is a directory!!"
elif [ -L "$file" ]
then
ECHO 031 "$file is a softlink file"
else
ECHO 031 "$file is not exist!!"
fi
FILE
}
例2:进行文件的删除、添加、备份。
当输入filectl.sh -d file 进行删除;当输入filectl.sh -a file 进行添加;当输入filectl.sh -p file 进行备份
1.0版本
使用$1 $2来进行字段的区分,从而实现直接输入选择
该版本只有简单的添加,删除,备份功能,健壮性不强。
if [ "$1" = "-a" ]
then
touch $2
elif [ "$1" = "-d" ]
then
rm -f $2
elif [ "$1" = "-p" ]
then
cp $2 /mnt
else
echo "Please input -a or -d or -p following filectrl.sh"
fi
2.0健壮性比较好
该版本达到了:
sh fiulectl.sh -a|-b|-p file 对输入的控制,必须按照此格式输入
Check_File()
{
if [ -e "$1" ]
then
$2
$3
fi
}
if [ "$#" -lt "2" ] 进行判断:输入的字符如果小于2,则执行以下
then
echo "Error:Please input option [-a|-d|-p] and following filename $0"
exit 1
fi
if [ "$1" = "-a" ]
then
Check_File $2 "echo $2 is exit!!" exit
touch $2
elif [ "$1" = "-d" ]
then
Check_File $2 "rm -rf $2" exit
echo "$2 is not exist"
elif [ "$1" = "-p" ]
then
Check_File $2 "cp -rp $2 /mnt" exit
echo "$2 is not exist"
else
echo "Please input -a|-d|-p following $0!!"
fi
说明:3和4版本,没有完全按照题目意思做
3.0版本
read -p "Please input your choice: " CHOICE FILE
if [ "$CHOICE" = "-a" ]
then
touch $FILE
elif [ "$CHOICE" = "-d" ]
then
rm -f $FILE
elif [ "$CHOICE" = "-p" ]
then
cp $FILE /mnt
else
echo "Please input -a or -d or -p following filectrl.sh"
fi
4.0
read -p "Please input your choice: " CHOICE FILE
case $CHOICE in
-a)
[ -z "$FILE" ]&&{
echo please input filename
}
[ -e "$FILE" ]&&{
echo $FILE is exist!!
}||{
touch $FILE
}
;;
-d)
rm -f $FILE
;;
-p)
[ -e "$FILE" ]&&{
cp $FILE /mnt
}||{
echo $FILE is not exist!!
}
;;
*)
echo "Please input -a or -d or -p following filectrl.sh"
esac
四、case语句
if 语句是进行顺序执行的,即就是每执行一次脚本,那么每一条if语句都会执行一遍,那么当脚本比较长时,就会影响效率,此时,case语句,刚好可以弥补这个缺点。
case语句时进行选择即直接执行满足条件的语句,就会极大的提高效率。
语法:
case $1 in
a|A)
echo "B" ##当输入一个字段是a或者A,输出B
;;
B)
echo "A" ##输入一个字段是B时,输出A
;;
*)
echo "hello" ##当输入其他字符时,输出hello
esac ##结束标示
例:将上一个if语句的练习题,改成case语句。具体的题目如下:
判断文件类型
具体要求:输入文件或者目录,然后判断类型(判断普通文件,软链接,目录),直到输入exit后再退出判断,否则一直进行判断。
注:软链接文件生曾成:ln -s soucefile soft_link_file 源文件必须是绝对路径,生成的软链接文件任意
Check_File()
{
if [ -e "$1" ]
then
$2
$3
fi
}
if [ "$#" -lt "2" ] 判断输入字段少于2个则执行以下语句:
then
echo "Error:Please input option [-a|-d|-p] and following filename $0"
exit 1
fi
case $1 in
-a|-A|a|A)
Check_File $2 "echo $2 is exit!!" exit
touch $2
;;
d|D|-d|-D)
Check_File $2 "rm -rf $2" exit
echo "$2 is not exist"
;;
p|P|-p|-P)
Check_File $2 "cp -rp $2 /mnt" exit
echo "$2 is not exist"
;;
*)
echo "Please input -a|-d|-p following $0!!"
;;
esac
五、expect
expect 是自动应答命令用于交互式命令的自动执行
spawn 是 expect 中的监控程序,其运行后会监控命令提出的
交互问题
send
发送问题答案给交互命令
"\r"
表示回车
exp_continue 标示当问题不存在时继续回答下面的问题
expect eof 标示问题回答完毕退出 expect 环境
interact
标示问题回答完毕留在交互界面
set NAME [ lindex $argv n ]
定义变量
发用户信息:
Auto_Scp()
{
/usr/bin/expect <<EOF
spawn scp -r /mnt/yyz root@$1:/mnt
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for NUM in {30..39}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null
if [ "$?" = "0" ]
then
Auto_Scp 172.25.254.$NUM
fi
done