Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage method of for cycle, while cycle and until cycle under Shell

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article to share with you is Shell programming for loop, while loop, until loop and other statements syntax format and use method, the content is more basic detailed, there is a need for friends can refer to the next.

1. For the first time. for loop syntax structure 1) list == loop

List for loop: Used to execute a set of commands a known number of times

Basic syntax for variable in {list} do command command … done or for variable in a b c do command command done Example # for var in {1.. 10};do echo $var;done# for var in 1 2 3 4 5;do echo $var;done# for var in `seq 10`;do echo $var;done# for var in $(seq 10);do echo $var;done# for var in {0.. 10.. 2};do echo $var;done# for var in {2.. 10.. 2};do echo $var;done# for var in {10.. 1};do echo $var;done# for var in {10.. 1..- 2};do echo $var;done# for var in `seq 10 -2 1`;do echo $var;done2) without list loop

For loops without lists are executed with user-specified parameters and the number of parameters

Basic syntax for variable do command command … done Example #!/ bin/bashfor vardoecho $vardoneecho "script followed by $#arguments"3) C-like for loop basic syntax structure for(( expr1;expr2;expr3 )) do command command … donefor (( i=1;i> /tmp/ip_up.txt || echo "$ip.$ i is down" >> /tmp/ip_down.txtdone[root@server shell03]# time ./ ping.sh real 0m24.129suser 0m0.006ssys 0m0.005s

Extended Extensions: Shell Script Concurrency

Parallel execution: {program}& means to put the program in the background for parallel execution. If you need to wait for the program to finish executing before proceeding with the following contents, you need to add wait#!/ bin/bash#define variable ip=10.1.1#loop to ping host IPfor ((i=1;i/dev/null if [ $? -eq 0 ];then echo "$ip.$ i is ok" >> /tmp/ip_up.txt else echo "$ip.$ i is down" >> /tmp/ip_down.txt fi}&donewaitecho "ip is ok.... "[root@server ~]# time ./ ping.sh ip is ok... real 0m3.091suser 0m0.001ssys 0m0.008s6) Determine leap year

Requirement 3:

Enter a year to determine whether it is a wet year (years divisible by 4 but not 100, or divisible by 400 are leap years)

#!/ bin/bashread -p "Please input year:(2017)" yearif [ $[$year%4] -eq 0 -a $[$year0] -ne 0 ];then echo "$year is leap year"elif [ $[$year%400] -eq 0 ];then echo "$year is leap year"else echo "$year is not leap year"fi

##4. summary

FOR loop syntax structure FOR loop can combine conditional judgment and flow control statements do... The loop body can be a command set, plus conditional judgment and flow control control loop statement continue, skip this loop, continue the next loop break, jump out of the loop, execute the code outside the loop exit, exit directly from the program

Features: == Enter the cycle if the condition is true; exit the cycle if the condition is false ==

1. while loop syntax structure while expression do command... donewhile [ 1 -eq 1 ] or (( 1 > 2 )) do command command ... done

Cycle printing 1-5 digits

For circular printing: for ((i=1;i/dev/null if [ $? -ne 0 ];then echo "system date failed" |mail -s 'check system date' root@localhost else let count++ if [ $[$count0] -eq 0 ];then echo "system date successfull" |mail -s 'check system date' root@localhost && count=0 fi fisleep 3done There are more ways to write scripts above fisleep 3done

Features: == Enter the cycle if the condition is false; exit the cycle if the condition is true ==

1. until syntax structure until expression [ 1 -eq 1 ] (( 1 >= 1 )) do command command ... done

Print 1-5 numbers

i=1while [ $i -le 5 ]do echo $i let i++donei=1until [ $i -gt 5 ]do echo $i let i++done2. Application case 1) Specific requirements Use the until statement to create 10 users in batches, and the UID of stu1-stu5 users is 1001-1005 respectively; the home directory of stu6-stu10 users is/rhome/stu6-/rhome/stu102 respectively) Idea to create user statements useradd -u| useradd -d Use loop statement (until) to create users in batches until loop statement structure judges the first 5 and the last 5 conditional judgment statements of users 3) landing implementation #!/ bin/env bashif [ -d /rhome ];then echo "/rhome directory already exists"else mkdir /rhome echo "/rhome does not exist, completed creating"fii=1until [ $i -gt 10 ]do if [ $i -le 5 ];then useradd -u $[1000+$i] stu$i echo 123|passwd --stdin stu$i else useradd -d /rhome/stu$i stu$i echo 123|passwd --stdin stu$i filet i++done==================================================#!/ bin/bashi=1until [ $i -gt 10 ]do if [ $i -le 5 ];then useradd -u $[1000+$i] stu$i && echo 123|passwd --stdin stu$i else [ ! -d /rhome ] && mkdir /rhome useradd -d /rhome/stu$i stu$i && echo 123|passwd --stdin stu$i filet i++done

The above is the syntax format and use method of the for loop, while loop, until loop and other statements under Shell programming. I hope you can learn how to use these loops after reading this article. If you want to know more, please pay attention to the information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report