Summary of scattered shell usage

Source: Internet
Author: User

Shell is widely used during this period of employment. There are some scattered points, which should be noted here.


The IF usage in Shell differs greatly from that in C. It is generally used in combination with [] (currently I only used this method:>), for example, if [$ A-EQ 0]; then do... done. Brackets actually play a true role in judgment. Note that there must be spaces on both sides of [and.

[], When compared with a number, there are several Relational operators:

-Equal EQ

-Ne is not equal

-GT greater

-Ge is greater than or equal

-Lt is less

-Le is less than or equal

When compared with a string, there are the following operators:

= Equal

! = Not equal

Note: space must be added to both sides of the relational operator.

Loop in the second shell

The for loop can be used in the following ways:


For file in $ (LS); do

Echo $ File


$ (LS) uses ls as a command execution and the output result as an array value. Each element of the array is separated by a blank space.

2 similarC LanguageUsage

For (I = 0; I <10; I ++); do

Echo $ I


The variable in () can be not added with $, which is the same as that in C language.


For I in 0 1 2 3; do

Echo $ I



For I in {1 .. 3}; do

Echo $ I


While loop usage:

While condition expression; do

Loop body


Conditional expressions can have many forms, such as the for loop () method similar to the C language, or similar condition statements in IF.


Awk and sed are the most common commands for log analysis. Here is a brief introduction.

1 Basic Method: awk-F "delimiter" 'in in {} condition_1 {} condition_2 {} end {} 'input_file_1 input_file_2

2 begin {} and end {} are optional. In fact, begin and end are equivalent to special condition. Begin {} is the first operation to be executed when the awk starts. The statements are executed before the input file is processed. Some initialization operations are generally performed. End {} is the part that is executed after all input files are processed. It is often used in statistical applications.

3 condition is the condition checked before processing each line of input. If the condition is met, the statement in parentheses is executed. Condition can be a condition statement such as =,>, and <, or a regular expression such as VAR ~ /ABC/indicates the regular expression in the variable VAR match.

4 awk has some built-in variables. For example, $ n indicates the nth field in the row being processed (the field is separated by the separator after-F), and the field starts from 1. $0 indicates the entire row. Argind indicates the number of input files (which seems to be faulty in MAC), NF indicates the number of fields in the current row, and NR indicates the number of rows processed currently.

5 there is no order between condition (including begin and end). You can write end {} before begin.

6. There can be multiple input files at the end. awk processes the input files in the order of file names.

Four functions

Finally, let's talk about Shell functions. The function declaration method is as follows:

Function Foo ()


X = $1



Call method: Foo argv

The function can accept parameters and use $1, $2 in the function.

Note: In shell, the variables in the function are global variables!

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