###For 语句###
循环语句
for 循环的条件
do 执行的动作
done 循环结束的标志
循环条件:会依次执行满足每个条件,规定循环的次数
for循环的方法:
方法一:
for num in 1 4 5 ##循环 1 4 5三个数 ,num不一定只指数字还可以指字符
[root@localhost mnt]# vim for.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:45:57 #
#Description: #
# #
####################################
#!/bin/bash
for i in 1 4 5 ##循环的条件
do
echo $i #执行的动作
done #结束循环
[root@localhost mnt]# sh for.sh #运行脚本
1
4
5
方法二:
for num in {1…5} ##循环1-5
[root@localhost mnt]# vim for.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:45:57 #
#Description: #
# #
####################################
#!/bin/bash
for i in {1..5}
do
echo $i
done
[root@localhost mnt]# sh for.sh
1
2
3
4
5
方法三:
for num in seq 1 10
##seq方法可以指定步长
[root@localhost mnt]# cat for.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:45:57 #
#Description: #
# #
####################################
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done
[root@localhost mnt]# sh for.sh
1
2
3
4
5
6
7
8
9
10
[root@localhost mnt]# cat for.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:45:57 #
#Description: #
# #
####################################
#!/bin/bash
for i in `seq 1 2 10` ##制定1-10
do
echo $i
done
[root@localhost mnt]# sh for.sh
1
3
5
7
9
[root@localhost mnt]# vim for.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:45:57 #
#Description: #
# #
####################################
#!/bin/bash
for i in `seq 2 310`
do
echo $i
done
[root@localhost mnt]# sh for.sh
2
5
8
应用示例:
写一个10s倒计时的脚本
[root@localhost mnt]# cat count.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:38:06 #
#Description: #
# #
####################################
#!/bin/bash
for num in {10..1}
do
echo -n "After ${num}s is end "
echo -ne "\r"
sleep 1
done
[root@localhost mnt]# sh count.sh
写一个查看ip地址可以ping通并把ping同的ip地址放入一个文件的脚本
#!/bin/bash
#判断储存ip的文件是否为空
[ -e "/mnt/host_ip" ]&&{
echo /mnt/host_ip is exist!! #输出该文件已存在
echo "[O]verwrite [B]ackup [S]kip" #输出选择:覆盖 备份 跳过
read -p "Choose the action: " WORD #选择要执行的动作
ACTION=`echo $WORD | tr 'a-z' 'A-Z'` ##将执行的动作进行大小写转换
[ "$ACTION" = "O" ] && { #如果输入O动作
rm -fr /mnt/host_ip ##执行删除命令
}
[ "$ACTION" = "B" ] && { #如果输入B动作
mv /mnt/host_ip /mnt/host_ip.bak #备份文件
}
[ "$ACTION" = "S" ] && { ##如果输入S动作
exit 0 #退出
}
}
#循环ping 172.25.254.1-172.25.254.20的主机
for ip in {1..20}
do
ping -c1 -w1 172.25.254.$ip &>/dev/null && { ##ping 通抓取ip地址
echo 172.25.254.$ip >> /mnt/host_ip ##将ip地址追加到文件
}
done
[root@localhost mnt]# sh ip.sh ##执行脚本
[root@localhost mnt]# cat host_ip
172.25.254.1
172.25.254.2
172.25.254.3
172.25.254.4
172.25.254.6
172.25.254.7
172.25.254.8
172.25.254.10
172.25.254.11
172.25.254.12
172.25.254.13
172.25.254.14
172.25.254.15
172.25.254.18
172.25.254.19
172.25.254.20
###while语句###
while语句用于不断循环执行一系列命令
while格式为:
while 条件
do 执行的动作
done 循环结束
[root@localhost mnt]# vim westos.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-21 02:24:57 #
#Description: #
# #
####################################
#!/bin/bash
while true ##当时true
do
read -p "Please input word: " WORD ##执行动作
echo $WORD
done
[root@localhost mnt]# sh westos.sh
Please input word: hello
hello
Please input word: linux
linux
###until语句###
- until 循环执行一系列命令直到条件为true时停止
- until循环与while循环在处理方式上正好相反
- 一般while循环优于until循环,极少情况下until更加有利
until格式为
until 条件
do 动作
done 结束标志
[root@localhost mnt]# cat westos.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-21 02:24:57 #
#Description: #
# #
####################################
#!/bin/bash
until false ##until循环与while循环在处理方式上正好相反
do
read -p "Please input word: " WORD
echo $WORD
done
[root@localhost mnt]# sh westos.sh
Please input word: hello
hello
Please input word: monming
monming
Please input word: ^C
应用示例:
写一个分秒倒计时的脚本
[root@localhost mnt]# cat count.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-20 22:38:06 #
#Description: #
# #
####################################
#!/bin/bash
SEC=10 ##定义变量
MIN=1
for ((SEC=10;SEC>=0;SEC--)) #循环执行递减1的命令
do
while [ "$SEC" = "0" -a "$MIN" = "0" ] #当秒和分都为0时
do
exit 0 ##退出
done
while [ "$SEC" = "0" ] #当秒为0时
do
echo -ne "After $MIN:$SEC is end!! " #输出还有几分:几秒结束
echo -ne " \r "
sleep 1
SEC=59 #秒从59开始计算
((MIN--)) #分减1
done
echo -ne "After $MIN:$SEC is end!! "
echo -ne " \r "
sleep 1
done
###if语句###
if语句主要用于条件判断
if 语句的格式:
if 判断条件
then 满足上述条件要执行的动作
fi 语句结束的标志
if else (多个条件判断) 语法格式:
if 条件1
then
满足条件1要执行的动作
else
其他情况下执行的动作
fi
应用示例:
循环判度输入的内容是文件、目录、链接还是字符块…当输入exit退出(不区分大小写)
[root@localhost mnt]# cat checkfiletype.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-22 21:23:40 #
#Description: #
# #
####################################
#!/bin/bash
while true ##while循环
do ##执行的条件
read -p "please input filename[or exit for quit] : " FILE #输入文件名
if [ "$FILE" = "EXIT" -o "$FILE" = "exit" ] #判断当FILE是ecit时退出
then
echo bye
exit 0
elif [ -L "$FILE" ] #判断当FILE是L时
then
echo "$FILE is socketlink!!" ##输出$FILE是socketlink!!
elif [ -d "$FILE" ]
then
echo "$FILE is directory!!"
elif [ -f "$FILE" ]
then
echo "$FILE is common files!!"
else
echo "$FILE is not exist!!"
fi
done
选择一个动作[ a| d|p] 处理文件
[root@localhost mnt]# cat filectrl.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-22 22:22:59 #
#Description: #
# #
####################################
#!/bin/bash
[root@localhost mnt]# cat filectrl.sh
####################################
#Author: Linux #
#Version: 7.3 #
#Mail: Linux@toto.com #
#Create_Date: 2019-08-22 22:22:59 #
#Description: #
# #
####################################
#!/bin/bash
read -p "Please input your choose[a/d/p]: " WORD FILE
if [ "$#" -lt 2 ] ##判断脚本后面是否跟两个动作
then
echo "Error: Please input choose[a/d/p] and filename!!"
elif [ "$WORD" = "EXIT" -o "$WORD" = "exit" ] ##判断输出exit退出
then
echo bye-bye
echo 0
elif [ "$WORD" = "-A" -o "$WORD" = "-a" ] ##如果输入a,判断文件是否存在
then
[ -e "$FILE" ] && {
echo "$FILE is exist" ##文件已存在报错不存在就建立文件
}||{
touch /mnt/$FILE
echo "$FILE is created!!"
}
elif [ "$WORD" = "-D" -o "$WORD" = "-d" ]##如果输入d,判断文件是否存在
then
[ ! -e "$FILE" ] && {
echo "$FILE is not exist" ##文件不存在报错存在就删除文件
}||{
rm -f $FILE
echo "$FILE is deleted!!"
}
elif [ "$WORD" = "-P" -o "$WORD" = "-p" ]##如果输入p,判断文件是否存在
then
[ !-e "$FILE" ] && {
echo "$FILE is not exist"
}||{
cp $FILE /mnt/$FILE.cp
echo "$FILE is copyed!!" ##文件已存在报错不存在就复制文件
}
else
echo "please input -a|-d|-p"
fi
###case语句###
case 语句多为选择语句,可以用case语句匹配一个值一个模式,如果匹配成功执行相匹配的命令
case 语句在进行判断的时候是并发进行的,处理效率高(类似点明机制);if语句在进行条件判断的时候注意判断,效率低下
case语句格式:
case 变量 in
满足情况1)
执行的命令
;;
满足情况2)
执行的命令
;;
....
*) #其余状况
执行的命令
esac
脚本示例:
#!/bin/bash
ACTION()
{
echo "action a|d|p"
read -p "Please input your choose[a/d/p]: " WORD FILE
if [ "$#" -lt 2 ]
then
echo "Error: Please input choose[a/d/p] and filename!!"
elif [ "$WORD" = "EXIT" -o "$WORD" = "exit" ]
then
echo bye-bye
echo 0
case $WORD in
-a|-A)
[ -e "$FILE" ] && {
echo "$FILE is exist"
}||{
touch /mnt/$FILE
echo "$FILE is created!!"
}
;;
-d|-D)
[ ! -e "$FILE" ] && {
echo "$FILE is not exist"
}||{
rm -f $FILE
echo "$FILE is deleted!!"
}
;;
-p|-P)
[ !-e "$FILE" ] && {
echo "$FILE is not exist"
}||{
cp $FILE /mnt/$FILE.cp
echo "$FILE is copyed!!"
}
;;
*)
echo "please input -a|-d|-p"
esac
}
while true
do
ACTION
done