Use shell examples to learn the syntax of the loop structure

Source: Internet
Author: User
Through the shell example to learn the syntax of the loop structure: the so-called useful is to learn to use it all the time. it is useless if you forget it. On the other hand, it is very likely that the leaders have great expectations for you. So what we learn should be used frequently in practice... information & n use shell examples to learn the syntax of the loop structure: the so-called "useful" means learning to use it all the time. it is useless if you forget it. On the other hand, it is very likely that the leaders have great expectations for you. So what we learn is often used in practice as our own. Www.2cto.com is actually the best way to learn is to first touch the actual things and use practice to verify the theory. Write a script to write a required certificate. you can test and write it (it is better to have an output mark in the middle to see where the error is ). The following example is shown in the video of Miss Lin Xianyi. I wrote it down silently in the centos system to practice it. maybe I just learned it, I always get a lot of prompts after writing them down and executing them (of course these prompts are incorrect information, but they are helpful to my beginners, I hope that after reading the video from instructor Lin, you must write down the exercises on your own ). Here, I want to take a note after reading the video, which also helps me make better progress. Let's take a look at the example. www.2cto.com example 1: This script allows the user to input a directory. then, the script will first determine whether the directory is empty or not, if this is the case, the files under the Directory are listed first, and then the file type is determined one by one, and the file permissions are determined. Finally, it lists the types and permissions of objects in the user input directory. #! /Bin/bash # program: use for display a directory's filetype and permission # history: 2013-02-20 ASK first release PATH =/bin:/sbin:/usr/bin: /usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin export PATH read-p "please you input a directory:" dir if [-z "$ dir"-o! -D "$ dir"]; then echo "you have input nothing or $ dir is NOT a directory" exit 0 fi filen = 'ls $ dir' for filename in $ filen do [-c $ dir/$ filename] & filetype = "is character file" [-p $ dir/$ filename] & filetype = "is PIPE file" [-f $ dir/$ filename] & filetype = "is regular file" [-L $ dir/$ filename] & filetype = "is link file" [-S $ dir/$ filename] & filetype = "is sokkt file "[-d $ dir/$ filename] & Filetype = "is directory" [-B $ dir/$ filename] & filetype = "is block file" [-r $ dir/$ filename] & per = "$ per readable" [-w $ dir/$ filename] & per = "$ per writeable" [-o $ dir/$ filename] & per = "$ per executable "[-u $ dir/$ filename] & per =" $ per set UID "[-g $ dir/$ filename] & per =" $ per set GID "[ -k $ dir/$ filename] & per = "$ per set Sticky bit" echo "this file $ dir/$ filename is $ filetype, And permission is $ per "filetype =" "per =" "done Note: The first line is the environment in which the shell script runs. There are many runtime environments, including/bin/awk-f and others. The second line is the script information to explain what to do. Generally, the shell script starts with "#" and comments rows are not executed by shell. The third line is some information about the script, including the date and author, and version information. The fourth line refers to the path in which the script runs. For example, the echo commands in the script must indicate absolute paths. The road Sutra has a lot of important ones. The fifth line is to use the export command to make the path take effect in the global environment, not only the current user's current shell environment. * ************** All of the above are some preparations, however, it is best to develop a good habit of ***************. the seventh line is to use read to define an interactive variable. Wait for the user to enter the read function to read the screen value and pay it to the dir variable. The information in double quotation marks is displayed on the screen. * 8 to 11th is a simple if structure statement, when the conditions in the brackets are true, execute the program segments of the ninth and tenth rows. This structure starts with if and ends with fi. * ************************* The eighth row is a test case of test. Test and brackets play the same role. It can be written as test-z $ dir. There are two test command modes in the brackets. The two are or relations are represented by-o. For example, you can write test-z $ dir | test-d $ dir. -Z indicates whether to test whether it is null. if it is null, return true.-d indicates whether to test whether it is a directory. If you do not know whether the command will get your answer, you can try it in the command status. For example, if I want to test-d/root & echo "this/root is a directory" | echo "this/root is NOT a directory ". If this/root is a directroy is output on the screen, test is true, because the relationship between the two is related. The opposite screen outputs this/root is NOT a directory description | the previous one is false. Because | front and | the following relationship is OR. The eighth line indicates executing program segments 9 and 10 when the input directory is empty or not. The ninth row is the content in the output double quotation marks when the eighth row is true. the tenth row is the exit script execution. The row www.2cto.com 11th is the end sign of the if structure. The first row is a value assignment. Assign the value of ls to filen. Here there is an anti-quotation mark (that is, the key of the tilde under the ESC key) to execute the command in it. Remember the difference between double quotation marks and single quotation marks. * ** Rows 13th to 34th are in the for loop structure. 15th to 21st determine the file type, and 23rd to 28th determine the permission. This is a for loop structure. For Ends With done in the middle of the do program segment. ** If you don't talk about anything in the middle, let's talk about the main topics from 1, 23rd to 28th: per = $ per ...... It means that permissions are not just one type, so you must write them in this format to accumulate permissions. 2. the last time filetype and per were assigned NULL values, they were accumulated when these permissions and types were recycled. Example 2: This script illustrates the accumulation of 1-through different cycles. For example, for, while, and. Use while first. While is the program segment where do to done is executed when the condition is true. If it is false, exit the program segment loop. #! /Bin/bash # program: use while to add 1 to 100 # history: 2013-02-20 ASK first release PATH =/bin:/sbin:/usr/bin:/usr/sbin: /usr/local/bin:/usr/local/sbin :~ /Bin export PATH I = 0 s = 0 while [$ I-lt 100] do I =$ ($ I + 1 )) s = $ ($ s + $ I) done echo "1 + 2 + 3... + $ I = $ s "or use. The difference with while is that until exits the current loop when the condition is true. Be sure to distinguish it from while #! /Bin/bash # program: use until to add 1 to 100 # history: 2013-02-20 ASK first release PATH =/bin:/sbin:/usr/bin:/usr/sbin: /usr/local/bin:/usr/local/sbin :~ /Bin export PATH I = 0 s = 0 until [$ I-eq 100] do I =$ ($ I + 1 )) s = $ ($ s + $ I) done echo "1 + 2 + 3... + $ I = $ s "or use the for loop for execution. #! /Bin/bash # program: use for add 1 to 100 # history: 2013-02-20 ASK first release PATH =/bin:/sbin:/usr/bin:/usr/sbin: /usr/local/bin:/usr/local/sbin :~ /Bin export PATH s = 0 for (I = 0; I <= 100; I ++) do s = $ ($ s + $ I )) done echo "1 + 2 + 3... + $ I = $ s "##### after this line is executed, 1 + 2 + 3 will be output... + 101 = 5050. This 101 is with I ++ note: www.2cto.com first: first talk about the operation. The structure of the operation is $ (()). For example, a =$ ($ B + $ c) means to add the values of B and c to. Another example is (num = 2 #11111111). This is to convert the binary values of 8 to decimal. That is to say, double left parentheses (and double and parentheses) constitute the format of the operation. Multiply by a = $ ($ B * $ c )). Second: Let's talk about this eq, lt, gt, and so on. Remember that e is equal to, l is less than, and g is greater. Equal (equal to) greater than (greater than...) less than (less ). Third: Finally, let's talk about the second format of for, that is, the for (initial value; restricted value; step size) format mentioned in the last example. I ++ and ++ I are different. you can check them online. Review: 1. Structure: if, for, while, until, and their differences 2. test and link statements: test and [], Link Processing &, | ,! -A,-o, etc. 3. value assignment: read-p, direct value assignment, and repeated value assignment. 4. quotation marks: single quotation marks, double quotation marks, and reverse quotation marks
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.