Shell Foundation and one of the process control statements while loop

Source: Internet
Author: User
Tags case statement

Before writing the IF condition to judge, generally in writing the shell script may use the dead loop at this time need while statement, it is the best way to do the dead loop, before the while loop need to tell the file test, because it is often used in writing scripts.

File test:

-E $file: Determine if there is true if present, false if not present

-F $file: Determine if it exists and is a normal file, exists as true, does not exist as false

-D $file: To determine if it exists and for the directory, existence for true does not exist for false

-H $file: Determines if there is a link file (soft connection), exists as true, does not exist as false

-L $file: no explanation as-H

-B $file: Determine if it exists and is a block device file, exists as true, does not exist as false

-C $file: Determine if there is a character device file, exists as true, does not exist as false

-S $file: Determines if there is a socket file, exists as true, does not exist as false

-P $file: Determine if it exists and is a pipe file, exists as true, does not exist as false

-R $file: Determines whether the current user has Read permission to this file

-W $file: Determines whether the current user has write access to this file

-X $file: Determines whether the current user has permission to perform this file

-U $file: Determine if the file has suid permissions

-G $file: Determine if the file has Sgid permissions

-K $file: Whether the file has sticky permissions

-O $file: Whether the current user is the owner of the file

-G $file: Whether the current user is a group of files

-N $file: Whether the file has been modified since the last time it was read

I feel so much better. I have not used all the script now, I have all taken notes, because the feeling will be used later and if you forget not to flip Google on the wall.

Now let's talk about the while loop, which is similar to the normal loop first.

The fixed format is:

While loop condition; do

Looping content

Modifying the expression of a cyclic variable

Done

The reason for this modification of the loop variable is that the loop will not exit if it is not re-assigned to the variable at the end of the loop after the cycle has passed. You must turn the loop condition to false to end the loop. Let's see an example.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/73/wKiom1SCySTTDDtUAAEbx15QkOc234.jpg "title=" 2014-12-06 17:15:04 screen. png "alt=" wkiom1scysttddtuaaebx15qkoc234.jpg "/>

I define the variable A as a numeric value and the initial value is 0, and the following while loop condition is if the value of $ A is less than 100 then the output hello I don't fix the value of $ A for the fixup expression, so the program will always output hello and will not stop. If you want to stop, you can stop the script by pressing CTRL + C

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/73/wKiom1SCyZXAC_eQAAFBCa9wCeU015.jpg "title=" 2014-12-06 17:16:52 screen. png "alt=" wkiom1scyzxac_eqaafbca9wceu015.jpg "/>

Let's modify the script to add a modifier expression. I added a let a++ the program loops once so that a value of $ A is added 1 until $ A value is greater than 100 the program ends.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/73/wKiom1SCyf3iRm60AAEXzOcdo8I496.jpg "title=" 2014-12-06 17:18:31 screen. png "alt=" wkiom1scyf3irm60aaexzocdo8i496.jpg "/>

Take a look at the implementation of the program results, exactly 100 lines of content because it is 0-99, the 100th time a value of $ A is not less than 100, and 100 is equal so the program ends, so that this correction expression is necessary otherwise will fall into the dead loop, if you want to do a dead loop below will also talk about an easier way.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/71/wKioL1SCyvuxL2IJAAF6_IcmXhQ597.jpg "title=" 2014-12-06 17:20:20 screen. png "alt=" wkiol1scyvuxl2ijaaf6_icmxhq597.jpg "/>

By the way, the file tests listed above are written in conditional judgment like [-F/ETC/PASSWD], which is to judge whether/ETC/PASSWD exists and continues to execute for ordinary files.

The dead loop is simple. Its format is:

While True;do

Looping content

Done

If you use the above example, the content is:

While True;do

echo "Hello"

Done

The general dead loop is used to interact with the user such as the user enters an incorrect content program that always prompts him to enter the content this uses a dead loop. The dead loop can also exit, of course, under certain conditions, such as when the user enters the correct content, you can execute the following content or exit the script.

Loop control command: Break, continue, exit

Break: Early Exit loop

Continue: Exits the current loop and goes directly to the next loop

Exit: When the script touches eixt, whatever happens next, exit the script directly.

Use examples to explain it:

0X01: Determine if a user is logged in or not, and if logged in, the output user logs in and exits the script, and loops if not logged in.

$!/bin/bash

While True;do

If who | grep "Test" &>/dev/null;

echo "User test is Logging"

Break

Fi

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/72/wKioL1SC25Dx1yeGAAFD1YajoV8156.jpg "title=" 2014-12-06 17:41:08 screen. png "alt=" wkiol1sc25dx1yegaafd1yajov8156.jpg "/>

Here the test user does not exit until the test user logs on, and the program returns to user test is logging and then exits.

0X02: Determine if the/tmp/root.txt file exists, output "file is found" if present, otherwise loop until the file exists

While True;do

if [-f/tmp/root.txt];then

echo "File is found"

Break

Fi

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/72/wKioL1SC3TTAcrdKAAEnGaJKe1c378.jpg "title=" 2014-12-06 18:38:12 screen. png "alt=" wkiol1sc3ttacrdkaaengajke1c378.jpg "/>

Like 0x01, the main thing is to look at how the file test options are used. This article is written here, may be a little bit less after all, these things are not just a look at it, or hope to operate more

The next article will refer to the case statement, a streamlined if statement, which is often used by nested IF statements in case statements, and of course these loop control statements can be nested arbitrarily (without error).

This article is from the "Yxn" blog, make sure to keep this source http://jyxnt.blog.51cto.com/9581167/1587137

Shell Foundation and one of the process control statements 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.