Shell script programming judgment Statement _linux Shell

Source: Internet
Author: User
Tags case statement

One, single branch if statement

Format: if judge condition; then

Statement1
statement2 ...
Fi

Two, two-branched if statement

Format: if judgment condition; Then

Statement1
statement2 ...
else
statementn
...
Fi

Use a previously used script to illustrate this structure.

Three, multiple branch if statement

Format: If Judge condition 1;then

Statement1 ...
Elif judgment Condition 2; Then
statement2 ...
Elif judgment Condition 3; Then
statement3 ...
else
statement4
...
Fi

Iv. Case Statement

Format: Case variable in

PATTERN1)
statement
...
;
PATTERN2)
statement
... 
;
*)
statement
...
;
; Esac


Because the judgment structure statement is relatively simple, did not do too much explanation, just remembers each statement the structural usage can.

Supplemental Shell Basic Syntax

1.1, Shell file at the beginning

The shell file must start with the following line (must be on the first line of the file):
#!/bin/sh

The symbolic #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use/BIN/SH to execute the program.

When you edit a good script, you must also make it executable if you want to execute the script.

To make the script executable:

Run chmod +x filename so you can use./filename to run

1.2 Notes

In shell programming, comments are expressed in sentences that begin with # until the end of the line. We sincerely recommend that you use annotations in your programs.

If you use annotations, you can understand how the script works and work in a very short period of time, even if you haven't used it for a very long time.

1.3 Variables

In shell programming, all variables are composed of strings, and you do not need to declare the variables, direct assignment can be, the use of variables, the form of $+ variable names.

To assign a value to a variable, you can write this:

 
  

Now print the contents of variable a:

echo "A is:" 
echo $a

Sometimes variable names can easily be confused with other words, such as:

num=2 

This does not print out "This is the 2nd" and only prints "This is the" because the shell searches for the value of the variable numnd, but the variable has no value. You can use curly braces to tell the shell what we want to print is the NUM variable:

num=2 

This will print: This is the 2nd

1.4 Environment variables

Variables processed by the EXPORT keyword are called environment variables. We do not discuss environment variables, as we typically simply log in
Use environment variables in scripts.

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.