Linux Shell script Quick Start

Source: Internet
Author: User

In this article, through the summary of variable declaration, loop statements, condition judgment, and data superposition in shell scripts, most scripts of Linux daily management can be solved.

1. Variable declaration:

Is a weak language, with name = xxx; $ name when getting the variable value

 

2. Condition judgment:

If [condition]; then

Operator;

Elif [condition]; then

Operator;

Else

Operator;

Fi

3. Loop:

While [condition]

# Condition if [] is used to replace test, there are two []

Do

Operator;

Done;

 

For (I = 1; I <100; I ++ ))

Do

Operator;

Done;

 

For VaR in $ (seq n)

# Or for VAR in 'cat/etc/hosts'

Do

Operator;

Done;

4. Note the following when using "[]" for judgment:

1) Each component must be separated by a space. If @ is used instead of a space, the expression is judged as @ [@ str1 ==@ str2 @] @;

2) When using '=' for determination, the two sides must be strings, so they must be enclosed by "", for example, "$ name" = "want ", an error occurred with $ name = "want". The types on both sides are different;

3) test judgment:

File Detection: Test-e filename

Test-f filename

Test-D direct

 

File Permission judgment:

Test-r filename

Test-W filename

Test-x filename

 

Document time:

Test file1-nt file2; Determine whether file1 is newer than file2

Test file1-ot file2;

Test file1-et file2; checks whether two files are the same file and can be used in hard link.

 

Two integers:

Test N1-EQ N2;

Test N1-ne N2;

Test N1-GT N2;

Test N1-lt N2;

Test N1-ge N2;

Test N1-Le N2;

 

 

String judgment:

Test-Z string; whether the string is null is 0. If the string is null, the value is true;

Test-N string; whether the string is not 0

Test ST1 = st2;

Test ST1! = St2;

 

Multiple judgments:

-A: and, for example, test-r file-a-X file;

-O:

! : Test! -X file: determines whether the file has no executable permissions.

 

4) [] judgment can be used instead of test for judgment, except that 2) strings must be equal to =;

 

5), let "Count = $ count + 1"; "" must exist; otherwise, an error occurs.

5. Character operations

I = 2;

I = $ ($ I * 2 ));

6. awk:

Awk 'condition type 1 {Action 1} Condition Type 2 {Action 2} 'filename

Print the first and 4th columns with the third column separated by semicolons greater than 10 in the file.

 

Cat file | awk 'in in {FS = ":"} $3> 10 {print $1 "\ t" $4 }}

 

7. Simple Example:

After reading this, can you write a script like this: use recursion to find the script with the file size exceeding the given size in the given directory, although you can run the find command, but it is worth doing it!

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.