QuickStart Shell Scripting (iii)

Source: Internet
Author: User

Learn from this section the main control statements, loop statements

Comments, Shell Multiline comments

The Shell line comment is simple, just add # at the beginning of each line, but he does not provide a special multi-line comment method, so we use a clever way to multi-line comments, using the EOF delimiter to multi-line comments, as follows:

  1 #! /bin/sh  2  3 file= "/desktop/file.test"  4  5 <<eof  6 If [-R $file]  7 then  8         echo "  1 "  9 fi Ten If [-W $file] and then         echo" 3 "-Else-         echo" 4 "-Fi-EOF-20 if [-F $file] Then         echo "file was an ordinary file" on fi

5-17 lines in the above code are commented out.

If Judgment statement

If judgment statements should already be familiar, many of the previous examples use the IF Judgment statement, which is directly on both programs and provides the result of the operation.

  1 #!  /bin/sh  2  3 var= "test"  4  5 if [$var = = Test1]  6 then  7         echo "1"  8 else if [$var = = Test ]  9 then ten         echo "2" one fi
Run Result: 2

The structure used above is

If ...

Then ...

else If ...

Then ...

Fi

Fi

Another type of structure is:

  1 #!  /bin/sh  2  3 var= "test"  4  5 if [$var = = Test1]  6 then  7         echo "1"  8 elif [$var = = Test]  9 then ten         echo "2" one fi
The result of the operation is also 2, this structure is similar to the above, just the middle of the else if ... fi was changed to Elif.

Looping statements

For loop

Let's look at an example:

  1 #! /bin/sh  2  3 for ((i=0; i<10; i++))  4 does echo $i  5 Done
At first glance it's still familiar, but there are a few points to keep in mind:

1,for loops are enclosed in double brackets, i.e.: ((...; ...; ...;))

2,do and done are essential.

The output is from 0 to 9 all numbers

While loop

  1 #! /bin/sh  2  3 i=0  4 while ((I<10))  5 does  6         echo $i  7 let         i++  8 Done
The loop has the same effect as the For loop result output above.

The structure of the while loop is:

While ...

Do ...

Done
Until cycle

The until loop is rarely used, and is structured like A/C + + Java do-while loop, but the actual condition is the terminating condition rather than the execution condition inside C + +. Here is a brief introduction, personal suggestions can be browsed, can also skip.

Structure:

Until ...

Do ...

Done

  1 #! /bin/sh  2  3 i=0  4 until ((i>=10))  5 do  6         echo $i  7 let         i++  8 Done
The above loop execution results are the same as before, please note that the condition inside the until is the exit condition.

Array
Before looking at the for and case statements, let's look at the shell array

Presentation mode:

Mode one: arraynum= (1 2 3 4 5)

Way two:

Arraynum[0]=0

Arraynum[1]=1

arraynum[2]=2

How to get the length of the array: ${#arraynum [@]} (arraynum[@] actually arraynum the list, counting the symbols we talked about earlier, so this expression is not difficult to understand)

In statement

And then we'll see how to iterate through the output array

  1 #! /bin/sh  2  3 arraynum[0]=a  4 arraynum[1]=b  5 arraynum[2]=c  6  7 for Var in ${arraynum[@]}  8 Do  9         echo ${var}-Done one-echo ${#arraynum [@]} echo ${arraynum[@]}

This is used for the in statement to traverse the output array content, the last two sentences to verify that I said above, ${#arraynum [@]} is the length of the computed array, ${arraynum[@]} is an array list.

Let's take a look at the output:


The structure of the for in statement is as follows:

For variable name in list
Do ...
Done

The expression statement is more commonly used, and we need to memorize his form and usage.


QuickStart Shell Scripting (iii)

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.