while 循环

需求1:使用while读入文件方式,批量创建用户

[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


需求2:使用while读入文件方式,批量创建用户以及密码

[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


需求3:猜数字游戏

[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 忽略本次循环剩余的所有代码,直接进行下一次循环,直到循环结束,然后继续循环之后的代码。

需求4:循环嵌套continue,打印1-9当数值为5则跳过本次循环,继续下一次循环。请分别使用for和while实现。

需求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

需求5: 使用while读入文件方式,批量创建用户以及密码 文件格式: username:password

[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

需求6: 猜数字游戏

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 )) 次"

需求7: 在一个2000多行数据文件,然后有10个新的文件,1-5行数据放到第一个文件里,6-10行数据 放到第二个文件里…46-50数据放到第10个文件里。然后51-55数据在放到第一个文件里。如何实现?【可忽略】

[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

posted @ 2019-11-03 16:50  爱可耐  阅读(287)  评论(0编辑  收藏  举报