Detailed introduction to basic Linux Shell script Learning (4)

Source: Internet
Author: User

In the previous basic Linux Shell script study, we talked about if, select, and Case of the process in the Linux Shell script. Here we will introduce the loop and quotation marks of the Linux Shell script control process, the control process contains many contents, and some of the content is about the here document.

4. Loop

Loop expression:

While...; do

....

Done

While-loop will run until the expression test is true. Will run while the expression that we test for is true.

The keyword "break" is used to jump out of the loop. The keyword "continue" is used to directly jump to the next loop without executing the remaining part.

The for-loop expression is used to view a string list (strings are separated by spaces) and then assigned to a variable:

For VaR in...; do

....

Done

In the following example, ABC is printed to the screen:

#! /Bin/sh

For var in a B C; do

Echo "Var is $ Var"

Done

The following is a more useful script showrpm. Its function is to print statistics of some RPM packages:

#! /Bin/sh

# List a Content summary of a number of RPM packages

# Usage: showrpm rpmfile1 rpmfile2...

# Example: showrpm/CDROM/RedHat/RPMS/*. rpm

For rpmpackage in $ *; do

If [-R "$ rpmpackage"]; then

Echo "=====================$ rpmpackage ===================="

Rpm-qi-p $ rpmpackage

Else

Echo "error: cannot read file $ rpmpackage"

Fi

Done

The second special variable $ * is displayed, which contains all input command line parameter values.

If you run showrpm OpenSSH. RPM w3m. RPM webgrep. rpm

$ * Contains three strings: OpenSSH. rpm, w3m. rpm, and webgrep. rpm.

5. quotation marks

ForwardProgramBefore passing any parameters, the program will extend the wildcards and variables. Here, the extension means that the program will replace the wildcard (for example, *) with the appropriate file name, and replace the variable with the variable value. To prevent this type of replacement, you can use quotation marks: Let's take a look at an example. Suppose there are some files in the current directory, two JPG files, mail.jpg and tux.jpg.

1.2 compile shell scripts

# Ch #! /Bin/sh mod + x filename

Cho *. jpg is slow. Why ?. /Filename to execute your script.

This will print the result of "mail.jpg tux.jpg.

Quotation marks (single quotation marks and double quotation marks) will prevent such wildcard extension:

#! /Bin/sh

Echo "*. jpg"

Echo '*. jpg'

This will print "*. jpg" twice.

Single quotes are stricter. It can prevent any variable extension. Double quotation marks can prevent wildcard extension but allow variable extension.

#! /Bin/sh

Echo $ Shell

Echo "$ shell"

Echo '$ shell'

The running result is:

/Bin/bash

/Bin/bash

$ Shell

Finally, there is also a method to prevent this extension, that is, to use the Escape Character -- reverse oblique ROD:

Echo *. jpg

Echo $ Shell

This will output:

*. Jpg

$ Shell

Here is the Linux Shell script basics. the control process also includes the content of the here document for further analysis.

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.