当前位置:网站首页>Shell script for, while loop statements, price guessing games
Shell script for, while loop statements, price guessing games
2022-07-19 02:47:00 【For whom do the stars change】
1、for sentence
(1)for structure
You need to specify a variable and a list of possible values , Repeat the same command sequence for each different value . Until the variable value is used up, the exit loop needs to be specified in advance
for Variable name in Value list
do
Command sequence
done
(2) Application example
Add and delete accounts according to the name list
Specify employee list file users.txt, Repeat adding users , Set initialization password
add to
vim /root/users.txt ( List file used as a test )
( Add three users )
LS
QY
AS
vim uaddfor.sh
#!/bin/bash
A=$(cat /root/users.txt)
for B in $A
do
useradd $B
echo "000000" | passwd --stdin $B &> /dev/null ( Set the password of each user to 000000)
done
chmod +x uaddfor.sh
./uaddfor.sh
tail -3 /etc/passwd ( View new users )
su user name ( Test login user )Batch delete user's script
vim uaddfor.sh
#!/bin/bash
A=$(cat /root/users.txt)
for B in $A
do
userdel -r $B &>/dev/null
done
chmod +x uaddfor.sh
./uaddfor.sh
tail -3 /etc/passwd ( View the user again to delete ) according to IP Address list check host status
from ipadds.txt Read the... Of each server in the file IP Address , repeat ping Connectivity testing , And input according to the test results
Give the corresponding prompt message .
vim /root/ipadds.txt ( List file used for testing )
192.168.1.10
192.168.1.20
192.168.1.30
vim ping.sh ( Cycle through the scripts of each host )
#!/bin/bash
A=$(cat /root/ipadds.txt)
for IP in $A
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is up."
else
echo "Host $IP is down."
fi
done
chmod +x ping.sh
./ping.sh2、while Loop statement
It is required to control the number of cycles 、 The operands are numbered in numerical order 、 Repeat operation according to specific conditions . It's more suitable for
Another cycle -while sentence .
(1)while Sentence structure
while Conditional test operation
do
Command sequence
done
Use while Loop statement . There are two special condition test operations , namely true( really ) and false( false ). Use true do
As a condition . It means that the condition is always true . The command sequence within the loop will continue indefinitely , Unless the script is forced to terminate exit Statement exit script ); conversely , If you use false As a condition . Then the loop body will not be executed . These two special conditions can also
Use in if Conditional test of statement .
(2)while Statement application example
Batch add and delete users with regular numbers
Increasing 20 Users
add to
vim uaddwhile.sh
#!/bin/bash
A="user"
i=1 ( Control the number of user name , The initial assignment is 1)
while [ $i -le 20 ]
do
useradd ${A}$i
echo "000000" | passwd --stdin ${A}$i &> /dev/null
let i++ ( Equate to i= expr $i +1 Make variable i The value of the increase 1)
done
chmod +x uaddwhile.sh
./uaddwhile.sh
grep "user" /etc/passwd | tail -3 ( Check whether the last three lines are incremented by 20 individual )Delete
vim uaddwhile.sh
#!/bin/bash
A="user"
i=1
while [ $i -le 20 ]
do
userdel -r ${A}$i
let i++
done
./uaddwhile.sh
id user20
It shows that if there is no such user, it will succeed Price guessing game
A random price number is generated in advance by the script (0-999) As the actual price , Judge user guesses
Through environment variables RANDOM You can get a value less than 2 Of 16 A random integer to the power of
Calculate its relation with 1000 You can get the remainder of 0-999 Random price
You can guess repeatedly by true As a test condition while Cycle to achieve
End the cycle when the user guesses the actual price : The process of judging the predicted price and the actual price adopts if Statements for , Embedded
Set on while Circulating body ; Use variables to record the number of guesses .
vim jiage.sh
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo " The actual price range of the goods is 0-999, Guess what it is ?"
while true
do
read -p " Please enter the number of prices you guessed :" INT
let TIMES++
if [ $INT -eq $PRICE ]
then
echo " Congratulations, you're right , The actual price is $PRICE"
echo " You guessed a total of $TIMES Time "
exit 0
elif [ $INT -gt $PRICE ]
then
echo " It's too high !"
else
echo " It's too low !"
fi
done
chmod +x jiage.sh
./jiage.sh ( After executing the command, you will enter 0-999 Previous figures )边栏推荐
- Reflection and Discussion on time management methods
- Understanding: what is interface and the concept of interface
- status 500 reading AftersaleService#getAftersaleList(Long)+com. sun. proxy.$ Proxy214.getAftersaleList
- Chapter 1 - multi agent system
- Leetcode 1: Two Sum
- Simple use case writing specification
- Common English business mail phrases
- RIP综合实验
- 使用Grafana8.5.2显示zabbix6.0的信息
- MySQL初探
猜你喜欢

Response assertion of JMeter interface test

HCIA_OSPF实验

安装软件提示无法定位程序输入点AddDllDirectory于动态链接库Kernel32.dll上(文末有下载地址)

Shell programming specifications and variables

InnoDB, MySQL structure, and the difference between the three kinds of deletion

OSPF综合实验

学习网络基础

PowerStor500T报错0x01806803

Chapter 1 - multi agent system

摇摆摇摆~防火墙
随机推荐
使用Grafana8.5.2显示zabbix6.0的信息
A preliminary study of MySQL
No, no, No. yesterday, someone really didn't write binary enumeration
This article only commemorates the modulus of negative numbers
[unity development tips] unity mixer mixer controls global volume
Swagger -- the most popular API framework in the world
2022.6.28-database-1 Isolation level of database
TCP的三次握手与四次断开
Flyway的SaaS多租户实现方案
Longest ascending subsequence - Optimization
Experience in using flow playback tool Gor
时间管理方法的反思与探讨
C语言回调函数 & sprinf 实际应用一例
Lamp platform deployment and Application
module_init函数底层原理
Leetcode --- one question per day
MDK Keil/ARM printf 用法说明
[solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti
Leetcode 1: Two Sum
Dirty reading, unreal reading, non repeatable reading