Linux Learning Summary (59) shell script 3-for while loop

Source: Internet
Author: User
Tags python script

1 for Loop

Syntax: for variable name in condition; ; done
Example 1

#!/bin/bashsum=0for i in `seq 1 100`;dosum=$[$sum+$i]doneecho $sum

Example 2
Find all files with the suffix. txt in the/123 directory
Bulk Modify. txt to. txt.bak
Analysis:
There are two ways to find a combination of the Xarge command and the other for loops.
Method One:

#!/bin/bashfind /123/ -type f -name "*.txt" |xargs -i mv{} {}.bak

Method Two:

#!/bin/bashcd /123/for f in `ls *.txt`;domv $f $f.bakdone
2 while Loop

Syntax: while condition; do ...; Done

#!/bin/bashsum=0  i=1    while [ $i -le 100 ]     do     sum=`expr $sum + $i`     i=`expr $i + 1`     doneecho $sum

Usually we write a monitoring script that can be used to make a dead loop.
Check the system load every half-minute and send an email notification when the load is above 10 o'clock.

#!/bin/bashwhile truedo load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d ‘.‘ -f1` if [ $load -gt 10 ]  then  /usr/lib/zabbix/alertscripts/mail.py  [email protected]  "load is high"  "load is high: $load" fi sleep 30done

Note:
The W command is used to view the system load,
Head-1 shows the first row, W |head-1 is equivalent to the command uptime
Awk-f ' Load average: ' {print $} ' means to load average: cut for delimiter, print second paragraph
Cut-d '. '-F1 indicates that the first paragraph is printed with the dot as the cutting character
Note: We add a space behind the load average: The result will not have a space in front of it. If you don't add a space, you can also take the SED and remove the space.
The above command is equivalent tow|head -1|awk -F ‘load average:‘ ‘{print $2}‘|cut -d ‘.‘ -f1 |sed ‘s/ //‘
Send an email notification with a Python script when the payload exceeds 10, in the form of a script followed by a mailbox name, subject, content.
For a single character as a delimiter, cut can implement AWK's cut-to-print functionality, but cut does not support strings as a cut character.

3 Continue in the loop

Ignore the operation of this cycle and go directly to the next loop.

#!/bin/bashfor i in `seq 1 5`;do    if [ $i -eq 3 ];then    continue    fi    echo -e "$i \c"done

Running results of 1 2 4 5, continue successfully skipped the operation with a loop variable of 3.

4 Break in the loop

Hit the break statement and jump out of the current loop directly.

#!/bin/bashfor i in `seq 1 5`;do    if [ $i -eq 4 ];then        break    fi    echo $idone

The run result is 1 2 3. When looping to 4 o'clock, it jumps out of the loop and no longer executes any statements in the loop.

5 Exit statement

Hit the exit statement and end the script directly.

#!/bin/bashfor i in `seq 1 5`;do    if [ $i -eq 4 ];then    exit    fi    echo $idoneecho "hello world"

With the result of 1 2 3, we found that the Echo statement outside of the last loop was not executed, and if you switched the exit to break, there would be an extra word for Hello World

6 Interactive repeated input case with while implementation

Requirements: Prompt the user to enter a number, if not input, prompt input, if the input is not a pure number, then prompted to enter a number until the correct input until

#!/bin/bashwhile [ 1 ]do    read -p "please input a number:" n    if [ -z $n ];then    echo "please input sth"    continue    fi    n1=`echo $n|sed ‘s/[0-9]//g‘`    if [ -n "$n1" ];then    echo "please input a pure number"    continue    fi    breakdoneecho $n

Note: While the dead Loop realizes repeated interaction, the first if judgment, when the user does not enter the character, then satisfies the condition that the-Z variable value is empty, and gives the hint, needs to enter the character. Execute continue at the same time, cycle from the beginning, asking for user input again.
When the user enters a non-numeric, satisfies the second if condition, gives the hint, needs to enter the pure number, simultaneously executes the continue, the cycle starts from the beginning, asks the user to enter again.
When the user correctly enters, executes the break statement directly, jumps out of the loop, and outputs the number entered by the user. While [1] can be swapped for while: or while true

Linux Learning Summary (59) shell script 3-for while loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.