一、 if语句
1,if
#!/bin/bash
user=kiosk
if grep $user /etc/passwd;then
echo “Hello $user”
fi
if date;then
echo “Hello date”
fi
2.if-else
#!/bin/bash
user=student
if grep $user /etc/passwd;then
echo "The files for user
user
else
echo “$user not exist!”
fi
3.if-elif-else
#!/bin/bash
if [ $1 == “student” ];then
echo “Welcome $1”
elif [ $1 == “westos” ];then
echo “Welcome $1”
elif [ $1 == “kiosk” ];then
echo “Welcome $1”
elif [ $1 == “linux” ];then
echo “Welcome $1”
else
echo “You are not allowed!”
fi
二、 for循环
生成奇数序列
[root@server41 mnt]# vim for2.sh
for NUM in seq 1 2 10
do
echo $NUM
done
[root@server19 mnt]# sh for2.sh
1
3
5
7
9
10s倒计时
[root@server41mnt]# vim time.sh
#!/bin/bash
for ((a=10;a>0;a–))
do
echo -n " TIME $a " ##注意空格
echo -ne “\r”
sleep 1
done
三、while循环
创建用户westos{1…20},并修改密码为123456
[root@server41 mnt]# vim while.sh
#!/bin/bash
PREFIX=“westos”
i=1
while [ $i -le 20 ]
do
useradd
i
echo “123456” | passwd --studin
i &> /dev/null
((i++))
done
[root@server41 mnt]# ls /home/
student westos11 westos14 westos17 westos2 westos4 westos7
westos1 westos12 westos15 westos18 westos20 westos5 westos8
westos10 westos13 westos16 westos19 westos3 westos6 westos9