[root@manager while]# cat while-3.sh
#!/usr/bin/bash
while read user
do
id $user &> /dev/null
if [ $? -ne 0 ];then
useradd $user
echo "$user用户创建成功"
else
echo "$user用户已存在"
fi
done<user.txt
[root@manager while]# cat while-4.sh
#!/usr/bin/bash
while read user2
do
user=$(echo $user2|awk '{print $1}')
pass=$(echo $user2|awk '{print $2}')
id $user &> /dev/null
if [ $? -ne 0 ];then
useradd $user &> /dev/null
echo "$pass" |passwd --stdin $user
if [ $? -eq 0 ];then
echo "$user 用户创建成功"
fi
else
echo "$user 用户已存在"
fi
done<user2.txt
[root@manager ~]# cat cs.sh
#!/usr/bin/bash
sj=$[$RANDOM%100+1]
i=1
while true
do
read -p "请输入你要猜的数字:" Action
if [ $Action -eq $sj ];then
echo "恭喜你猜对了"
break
elif [ $Action -lt $sj ];then
echo "你猜小了"
elif [ $Action -ge $sj ];then
echo "你猜大了"
fi
let i++
done
echo "你总共猜了$i 次,失败了 $[$i -1] 次"
while 循环
while本质上就是循环
只不过while支持条件测试语句
整数比对
字符串比对
正则比对
文件比对
while读入文件的方式比较的特殊
while读入文件按行读取 IFS指定分隔符
for读入文件按空格读取 IFS指定分隔符
while read file
do
echo $file
done < /etc/passwd
循环中的控制语句 ----> for while
exit
break
continue
1.exit,退出整个程序。当脚本碰到exit时,直接退出,剩余不管有多少代码都不执行。
2.break,结束当前循环,但会执行循环之后的所有的代码。
3.continue 忽略本次循环剩余的所有代码,直接进行下一次循环,直到循环结束,然后继续循环之后的代码。
需求2:循环嵌套break,打印1-9当数值为5则停止。请分别使用for和while实现。
需求1: 使用while读入文件方式,批量创建用户
[root@manager while]# cat while-12.sh
#!/bin/bash
while read line
do
id $line &>/dev/null
if [ $? -eq 0 ];then
continue
else
useradd $line
fi
done < user.txt
[root@manager while]# cat while-13.sh
#!/bin/bash
while read line
do
user=$(echo $line | awk -F ":" '{print $1}')
pass=$(echo $line | awk -F ":" '{print $2}')
id $user &>/dev/null
if [ $? -eq 0 ];then
continue
else
useradd $user
echo "$pass" | passwd --stdin $user
fi
done <user.txt
1)随机输出一个1-100的数字 echo ((((((RANDOM%100+1))
2)要求用户输入的必须是数字(数字处加判断)
3)如果比随机数小则提示比随机数小了 大则提示比随机数大了
4)正确则退出 错误则继续死循环
5)最后统计猜了多少次(猜对了多少次,失败多少次)
[root@manager while]# cat while-14.sh
#!/bin/bash
SJ=$(($RANDOM%100+1))
i=1
while true
do
read -p "请输入你要猜的数: " Action
if [ $Action -eq $SJ ];then
echo "恭喜你 gdx...."
break
elif [ $Action -lt $SJ ];then
echo "太小了 gdx...."
else
echo "太大了 gdx...."
fi
let i++
done
echo "你总共猜了 $i 次, 失败了 $(( $i-1 )) 次"
[root@manager while]# cat while-15.sh
#!/bin/bash
while true
do
for i in $(seq 10)
do
head -5 file.txt >> file_$i.txt
sed -i '1,5d' file.txt
? if [ ! -s file.txt ];then
? exit
? fi
done
done
-------------------------------------------------------------------------------
#!/bin/bash
a=0
b=0
while [ $b -lt 2000 ]
do
file=num.txt
while [ $a -lt 10 ]
do
a=$[$a+1]
b=$[$b+5]
echo "$a $b"
line=$(awk "NR==$[$b-4],NR==$b" $file)
echo "$line">>${a}.txt
done
a=0
done
[root@baozexu ~/expect]# cat expect.ex
#!/usr/bin/expect
spawn ssh root@10.0.0.51
expect {
"yes/no" { send "yes\r"; exp_continue }
"password" { send "1\r" };
}
interact
exit 退出整个程序
break 结束当前循环,或跳出本次循环
continue 忽略本次循环剩余的代码,直接进行下一次循环
ab -n 200 -c 20 http://127.0.0.1/index.html
[root@baozexu ~/while]# cat rizhi.sh
#!/usr/bin/bash
Time=$(date +%F -d -1day)
File=/var/log/httpd/access_log
Log_dir=/var/log/httpd
mv $File $Log_dir/$Time-access_new_log
systemctl reload httpd
[root@baozexu ~/while]# cat while-1.sh
#!/usr/bin/bash
while [ -f /etc/hosts ]
do
echo ok
exit
done
[root@baozexu ~/while]# cat while-2.sh
#!/usr/bin/bash
while read line
do
echo $line
done<user.txt
[root@baozexu ~/while]# cat while-3.sh
#!/usr/bin/bash
while read test
do
echo $test
done</etc/hosts
[root@baozexu ~/while]# cat while-4.sh
#!/usr/bin/bash
while read uu
do
user=$(echo $uu |awk '{print $1}')
pass=$(echo $uu |awk '{print $2}')
id $user &> /dev/null
if [ $? -ne 0 ];then
useradd $user
echo "$pass" |passwd --stdin $user &> /dev/null
if [ $? -eq 0 ];then
echo "$user用户创建成功"
fi
else
echo "$user用户已存在"
fi
done<user.txt
原文:https://www.cnblogs.com/baozexu/p/11787851.html