大家好,又见面了,我是你们的朋友全栈君。
for 变量名 in 取值列表
do
循环体
done
[root@hdp-1 shell]# cat for_ping.sh
#!/bin/bash
for name in baidu 163 hao123
do
{
host=www.$name.com
ping -c1 -w1 $host &> /dev/null
if [ $? -eq 0 ];then
echo "$host" | tee -a host.txt
fi
}&
done
wait
echo "Mission Completed."
[root@hdp-1 shell]# cat users_from_file.sh
IFS=$'\n'
for line in `cat $1`
do
if [ ${
#line} -eq 0 ];then
continue
fi
user=`echo "$line" | awk '{print $1}'`
pass=`echo "$line" | awk '{print $2}'`
id $user &>/dev/nuil
if [ $? -eq 0 ] ;then
echo "user $user already exists"
else
useradd $user
echo "$pass" | passwd --stdin $user &> /dev/null
if [ $? -eq 0 ];then
echo "$user created"
fi
fi
done
while 条件测试
do
循环体
done
[root@hdp-1 shell]# cat while_user.sh
#!/bin/bash
while read line
do
if [ ${
#line} -eq 0 ];then
echo "NULL"
continue
fi
user=`echo $line | awk '{print $1}'`
pass=`echo $line | awk '{print $2}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exists."
else
useradd $user
echo "$pass"| passwd --stdin $user &>/dev/null
if [ $? -eq 0 ] ;then
echo "$user created ."
fi
fi
done < $1
until 条件测试
do
循环体
done
[root@hdp-1 shell]# cat until_ping.sh
#!/bin/bash
host=www.baidu.com
until ping -c1 -w1 $host &>/dev/null
do
sleep 1
done
echo "$host up. "
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163962.html原文链接:https://javaforall.cn