shell中的条件语句与循环语句

一、 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 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

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 P R E F I X {PREFIX} PREFIXi
echo “123456” | passwd --studin [ P R E F I X ] [PREFIX] [PREFIX]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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值