0、脚本中的函数
脚本中的函数是把一个复杂的语句块定义成一个字符串的方法
在调用函数时就把函数名写在想要调用的位置即可
- 例:
#!/bin/bash
Function_01()
{
read -p "please input your action: " Action
[ "$Action" = "exit" ] && exit 0
[ "$Action" = "user" ] && echo "You are $USER"
Function_01
}
Function_01
运行结果:
一、 if 语句
-
if 用法
if 条件 ; then 动作 fi
示例:
[root@fuwu test]# ls /home/
limin student westos
[root@fuwu test]# cat user.sh
#!/bin/bash
user=student
if grep $user /etc/passwd;then
echo "Hello $user"
fi
if date;then
echo "Hello date"
fi
[root@fuwu test]# sh user.sh
student:x:1000:1000:Student User:/home/student:/bin/bash
Hello student
Thu Dec 27 21:09:21 EST 2018
Hello date
- if - else 用法
if 条件 ; then
动作 1
else
动作 2
fi
示例:
[root@fuwu test]# cat if-else.sh
#!/bin/bash
user=linux
if grep $user /etc/passwd;then
echo "The files for user $user are:"
ls -a /home/$user
else
echo "$user not exist!"
fi
[root@fuwu test]# sh if-else.sh
linux not exist!
- if - elif - else 用法
if 条件 ; then
动作 1
elif
动作 2
else
动作 3
fi
示例:
[root@fuwu test]# cat if-el-if.sh
#!/bin/bash
if [ $1 == "student" ];then
echo "Welcome $1"
elif [ $1 == "westos" ];then
echo "Welcome $1"
elif [ $1 == "limin" ];then
echo "Welcome $1"
else
echo "You are not allowed!"
fi
[root@fuwu test]# sh if-el-if.sh student
Welcome student
[root@fuwu test]# sh if-el-if.sh linux
You are not allowed!
二、for循环
-
用法
for 条件 do 动作 done
-
示例1(从1-5排序的三种方法)
(1)第一种
[root@fuwu test]# cat for01.sh
#!/bin/bash
for NUM in `seq 5`
do
echo $NUM
done
[root@fuwu test]# sh for01.sh
1
2
3
4
5
(2)第二种
[root@fuwu test]# cat for02.sh
#!/bin/bash
for ((A=1;A<=5;A++))
do
echo $A
done
[root@fuwu test]# sh for02.sh
1
2
3
4
5
(3)第三种
[root@fuwu test]# seq 5
1
2
3
4
5
- 示例 2 :1-10内奇数排列
[root@fuwu test]# cat for01.sh
#!/bin/bash
for NUM in `seq 1 2 10`
do
echo $NUM
done
[root@fuwu test]# sh for01.sh
1
3
5
7
9
- 示例 3: 检测多台主机网络的通断
[root@fuwu test]# cat ping.sh
#!/bin/bash
for a in {1..160}
do
ping -c1 -w1 172.25.254.$a &> /dev/null && echo 172.25.254.$a is up || echo 172.25.254.$a is down
done
[root@fuwu test]# sh ping.sh
172.25.254.1 is down
172.25.254.2 is down
172.25.254.3 is down
172.25.254.4 is down
172.25.254.5 is down
....
172.25.254.60 is up
....
- 示例 4 :10秒倒计时
[root@fuwu test]# cat time.sh
#!/bin/bash
for ((a=10;a>0;a--))
do
echo -n " TIME $a "
echo -ne "\r"
sleep 1
done
[root@fuwu test]# sh time.sh
TIME 6
- 示例 5:分秒格式倒计时
[root@fuwu test]# cat time01.sh
#!/bin/bash
read -p "请输入需要倒计时的分和秒:" a b
for ((A=a*60+b;A>=0;A--))
do
c=$[A/60]
d=$[A%60]
echo -n " $c:$d "
echo -ne "\r"
sleep 1
done
[root@fuwu test]# sh time01.sh
请输入需要倒计时的分和秒:23 45
23:17
三、while循环
-
用法
while 条件 do 动作 done
-
示例 1:每两秒输出一次时间
[root@fuwu test]# cat while.sh
#!/bin/bash
while true
do
uptime
sleep 2
done
[root@fuwu test]# sh while.sh
21:42:11 up 38 min, 3 users, load average: 0.00, 0.01, 0.05
21:42:13 up 38 min, 3 users, load average: 0.00, 0.01, 0.05
21:42:15 up 38 min, 3 users, load average: 0.00, 0.01, 0.05
- 示例 2 :创建用户westos{1…20},并修改密码为123456
[root@fuwu test]# cat while01.sh
#!/bin/bash
PREFIX="westos"
i=1
while [ $i -le 20 ]
do
useradd ${PREFIX}$i ###删除用户是userdel -r ${PREFIX}$i &> /dev/null
echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null
((i++))
done
[root@fuwu test]# sh while01.sh
[root@fuwu test]# ls /home/
limin westos1 westos12 westos15 westos18 westos20 westos5 westos8
student westos10 westos13 westos16 westos19 westos3 westos6 westos9
westos westos11 westos14 westos17 westos2 westos4 westos7
四、嵌套循环
- 示例 1
[root@fuwu test]# cat qtxh.sh
#!/bin/bash
for((a=1;a<=3;a++))
do
echo "Starting outside loop: $a"
for((b=1;b<=3;b++))
do
echo "Inside loop: $b"
done
done
[root@fuwu test]# sh qtxh.sh
Starting outside loop: 1
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting outside loop: 2
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting outside loop: 3
Inside loop: 1
Inside loop: 2
Inside loop: 3
- 示例 2 :9*9乘法表
root@fuwu test]# cat cfb.sh
#!/bin/bash
for ((a=1;a<=9;a++))
do
for((b=1;b<=a;b++))
do
echo -ne "$a*$b=$[ a*b]\t"
done
echo -e "\n"
done
[root@fuwu test]# sh cfb.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81