当前位置:网站首页>Basic series of SHEL script (III) for while loop
Basic series of SHEL script (III) for while loop
2022-07-05 07:20:00 【jiankang66】
One 、 background
Although I am a java The programmer , Do the back end , But recently, I often need to read scripts written by others to run projects , So as a back-end programmer , We also need to know shell grammar , Can read some basic shell Script .
Two 、for loop
1、 There are three ways to write , Next, print 1 To 5 As an example
(1)、 The first way to write it , Achieve printing 1 To 5.
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
(2)、 The second way , Achieve printing 1 To 5.
for i in {1..5}
do
echo $i
done
(3)、 The third way , Achieve printing 1 To 5.
for((i=1;i<=5;i++))
do
echo $i
done
2、 Some keywords
break | Out of the loop |
exit | Exit script |
continue | continue , Jump out of this cycle |
3、 give an example , Judge whether it is a prime number
(1)seq The command is used to generate all integers from one number to another
# Enter a number when the command line executes
read -p " Please enter a number :" number
# Compare number Is it 1, by 1 Output is not prime , Exit procedure
[ $number -eq 1 ] && echo "$number Not prime " && exit
# Compare number Is it 2, by 2 The output is prime , Exit procedure
[ $number -eq 2 ] && echo "$number Prime number " && exit
# Ergodic 2 To number-1 Divide by 2 The integer of
for i in `seq 2 $[$[$number-1]/2]`
do
# Compare number Whether the module is 0, by 0 Not prime
[ $[$number%$i] -eq 0 ]&& echo "$number Not prime " && exit
done
echo "$number Prime number " && exit
4、 For example, create users in batch
(1)for Loop creation 5 Users .
# To etc/group Determine whether to use class The first group
grep -w ^class /etc/group &>/dev/null
# Judge whether there is a group according to whether the command in the previous step is successful , There is no add group
test $? -ne 0 && groupadd class
# Traverse creation u1 To u5 user
for((i=1; i<=5;i++))
do
useradd -G class u$i
# Set the password for the user
echo 123|passwd --stdin u$i
done
3、 ... and 、while loop
1、 There are two common ways to write , by true perform while loop
(1)、 The first way to write it [ Judging expressions ]
while [ 1 -eq 1]
do
command
done
(2)、 The second way ,(( Judging expressions ))
while (( 1< 2 ))
do
command
done
2、 give an example ,where Print 1 To 5
i=1
while (($i<=5))
do
echo "$i"
i=$[$i+1]
done
3、 give an example , Script synchronization system time
(1) Every time 30 Second synchronization system time , Alibaba cloud time synchronization server : ntp1.aliyun.com.
(2) Synchronous success , success 100 Send a success notification to your mailbox after times .
(3) Synchronization failure , Send failure notification to mailbox .
(4) We need to pay attention to if There is a space between and conditional judgment .
(5) No, mail command , adopt yum install sendmail install .
#!/bin/bash
NTP=ntp1.aliyun.com
count=0
while true
do
nptdate $NTP&>/dev/null
if [ $? -ne 0 ];then
echo "system date failed" |mail -s "check system time" [email protected]
else
let count++
if [ $count -eq 100 ];then
echo "system date success" |mail -s "check system time" [email protected] && count=0
fi
fi
sleep 1
done
(6) After execution , The results of the email received are as follows .
Four 、 summary
The above is about shell grammar for loop ,while Everything about the cycle , Hopefully that helped , You can refer to it , If you think it's good , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .
边栏推荐
- Rough notes of C language (2) -- constants
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
- Light up the running light, rough notes for beginners (1)
- Concurrent programming - how to interrupt / stop a running thread?
- 现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
- 并发编程 — 死锁排查及处理
- Rough notes of C language (1)
- Ros2 topic (VIII)
- 并发编程 — 如何中断/停止一个运行中的线程?
猜你喜欢
剑指 Offer 56 数组中数字出现的次数(异或)
Concurrent programming - deadlock troubleshooting and handling
DelayQueue延迟队列的使用和场景
M2DGR 多源多场景 地面机器人SLAM数据集
U-Boot初始化及工作流程分析
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
[software testing] 03 -- overview of software testing
Ros2 - workspace (V)
随机推荐
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
【无标题】
苏打粉是什么?
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
HDU1232 畅通工程(并查集)
[vscode] prohibit the pylance plug-in from automatically adding import
Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
Negative number storage and type conversion in programs
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
【obs】x264编码:“buffer_size“
IPage能正常显示数据,但是total一直等于0
[node] NVM version management tool
arcpy. SpatialJoin_ Analysis spatial connection analysis
Mipi interface, DVP interface and CSI interface of camera
And let's play dynamic proxy (extreme depth version)
网易To B,柔外刚中
U-Boot初始化及工作流程分析
Docker installs MySQL and uses Navicat to connect
Ugnx12.0 initialization crash, initialization error (-15)
C#学习笔记