SHELL_学习之路(6)循环和判断

######for######
for 条件
do
动作
done
示例:倒计时
#!/bin/bash
read -p “please input two numbers:” hour min sec
time= ( ( (( ((hour*3600+ m i n ∗ 60 + min*60+ min60+sec))
for ((a=$time;a>0;a–))
do
echo -n "Time ( ( (( ((a/3600)) h ( ( (( ((a%3600/60)) m ( ( (( ((a%3600%60)) s "
echo -ne “\r”
sleep 1
done
######while######
while 条件
do
动作
done
示例:创建westos(1-20),并设置其密码为123456
#!/bin/bash
PREFIX=“westos”
i=1
while [ $i -le 20 ]
do
userdel -r P R E F I X {PREFIX} PREFIXi &> /dev/null
echo “123456” | passwd --stdin P R E F I X {PREFIX} PREFIXi &> /dev/null
((i++))
done

######if-else######
if 条件;then
动作1
elif
动作2
elif
动作3
else
动作4
fi
查看用户是否存在:
#!/bin/bash
user=student
if grep $user /etc/passwd;then
echo "The files for user u s e r a r e : " l s − a / h o m e / user are:" ls -a /home/ userare:"lsa/home/user
else
echo “$user not exist!”
fi
#######多重循环############
示例:
打印99乘法表
#1
1=1
#21=2 22=4
#31=3 32=6 3*3=9

for i in $(seq 9)
do
for j in $(seq i ) d o e c h o − n e " i) do echo -ne " i)doechone"i* j = j= j=(( i ∗ i* ij))\t"
done
echo -e “\n”
done

你可能感兴趣的:(SHELL_学习之路(6)循环和判断)