当前位置:网站首页>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 .
边栏推荐
- [framework] multi learner
- Xiaomi written test real question 1
- Reading literature sorting 20220104
- Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
- 【软件测试】02 -- 软件缺陷管理
- [software testing] 02 -- software defect management
- Ugnx12.0 initialization crash, initialization error (-15)
- Logical structure and physical structure
- Unity ugui how to match and transform coordinates between different UI panels or uis
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
猜你喜欢
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
[software testing] 03 -- overview of software testing
Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
611. 有效三角形的个数
Jenkins reported an error. Illegal character: '\ufeff'. Class, interface or enum are required
And let's play dynamic proxy (extreme depth version)
SOC_ SD_ CMD_ FSM
Target detection series - detailed explanation of the principle of fast r-cnn
[software testing] 02 -- software defect management
随机推荐
Lexin interview process
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
2022.06.27_ One question per day
Negative number storage and type conversion in programs
What does soda ash do?
The difference between NPM install -g/-save/-save-dev
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
DelayQueue延迟队列的使用和场景
Binary search (half search)
Hdu1231 maximum continuous subsequence (divide and conquer or dynamic gauge or double pointer)
Raspberry pie 4B arm platform aarch64 PIP installation pytorch
Interpretation of the earliest sketches - image translation work sketchygan
611. 有效三角形的个数
Ros2 - ros2 vs. ros1 (II)
Reading literature sorting 20220104
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
What is sodium hydroxide?
docker安装mysql并使用navicat连接
SOC_ SD_ CMD_ FSM
U-Boot初始化及工作流程分析