Learn more about Linux Shell Scripting Basics (iv)

Source: Internet
Author: User
In a Linux shell script Basic Learning We talked about the Linux shell script control process of If, select, case, here is the Linux shell script control process loop and quotation marks, control process this part of the content is more, There is also a part of the content about here document.
4.loop
Loop expression:

While ...; Do....done

While-loop will run until the expression test is true. Would run while the expression, the We test for is true.
The keyword "break" is used to jump out of a loop. The keyword "Continue" is used to skip to the next loop without executing the rest of the section.
The For-loop expression looks at a list of strings (strings separated by spaces) and assigns them to a variable:

for Var in ...; Do....done

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

#!/bin/shfor var in A B C; Doecho "Var is $var" done

The following is a more useful script showrpm, which functions to print some RPM package statistics:

#!/bin/sh# List A content summary of a number of RPM packages# usage:showrpm rpmfile1 rpmfile2 ... # example:showrpm/cdr Om/redhat/rpms/*.rpmfor rpmpackage in $*; Doif [-R "$rpmpackage"];thenecho "=============== $rpmpackage ==============" Rpm-qi-p $rpmpackageelseecho "Error:cann OT read file $rpmpackage "Fidone

There is a second special variable, $*, that contains all the input command-line parameter values.
If you run showrpm openssh.rpm w3m.rpm webgrep.rpm
At this point $* contains 3 strings, namely openssh.rpm, w3m.rpm and webgrep.rpm.
5. Quotation marks
The program expands wildcard characters and variables before passing any parameters to the program. The so-called extension means that the program replaces the wildcard character (such as *) with the appropriate file name, and the variable is replaced with the variable value. To prevent the program from making this substitution, you can use quotation marks: Let's take a look at an example, assuming there are some files in the current directory, two JPG files, mail.jpg and tux.jpg.
1.2 Compiling shell scripts
#ch #!/bin/sh mod +x filename
Cho *.jpg∪ slow Teman tip Wine together Ltd U Chung Yuen oldest? /filename to execute your script.
This will print out the results of "mail.jpg tux.jpg".
quotation marks (single and double quotation marks) prevent this wildcard extension:

#!/bin/shecho "*.jpg" Echo ' *.jpg '

This will print "*.jpg" two times.
Single quotes are more restrictive. It prevents any variable expansion. Double quotes prevent wildcard expansion but allow variable expansion.

#!/bin/shecho $SHELLecho "$SHELL" Echo ' $SHELL '

The result of the operation is:

/bin/bash/bin/bash$shell

Finally, there is a way to prevent this extension by using the escape character-the backslash:

Echo *.jpgecho $SHELL

This will output:

*.jpg$shell

Linux Shell Scripting Basics Here, the control process has a little bit of here document content to analyze next time.

Above is the detailed introduction of the Linux shell Script Basic Learning (iv) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.