Shell programming in Linux (i)

Source: Internet
Author: User
Tags arithmetic

Conditional judgment, control structure:

Single Branch if statement

if judgment condition; Then

Statement1

Statement2

...

Fi

Two-branch if statement:

if judgment condition; Then

Statement1

Statement2

...

Else

Statement3

Statement4

...

Fi

Multi-Branched if statement:

if judgment condition 1; Then

Statement1

...

Elif judgment Condition 2; Then

Statement2

...

Elif judgment Condition 3; Then

Statement3

...

Else

Statement4

...

Fi

Test method:

    • [Expression]
    • [[Expression]]
    • Test expression

There are three common conditions tested in bash:

Integer test:

    • -EQ: Tests whether two integers are equal, such as $A-eq $B
    • -ne: Test whether the two integers are unequal, unequal, true, equal, false;
    • -GT: Tests whether one number is greater than the other, greater than, true, or false;
    • -LT: Tests whether one number is less than the other, less than, true; otherwise, false;
    • -ge: greater than or equal to
    • -le: Less than or equal to

Example:
int1=63
int2=77
[$INT 1-eq $INI 2]
[[$INT 1-eq $INT 2]]
Test $INT 1-eq $INT 2

File test:

    • -e file: Test files exist
    • -F File: Test files are normal files
    • -D FILE: Tests whether the specified path is a directory
    • -R File: Tests whether the current user has read access to the specified file;
    • -W
    • -X

Example: [-e/etc/inittab] [-x/etc/rc.d/rc.sysinit]

How to perform arithmetic operations in the shell:
A=3
B=6
1. Let arithmetic operation expression
Let c= $A + $B
2. $[arithmetic operation expression]
c=$[$A + $B]
3, $ (arithmetic operation expression)
c=$ (($A + $B))
4, expr arithmetic expression, the expression in the operands and operators to have a space between, and to use the command reference
c= ' expr $A + $B '

Defining script Exit Status codes

Exit: Exit Script Exit # If the script does not explicitly define exit status code, then the exit code of the last command executed is the exit status code of the script;

Test script for syntax error: bash-n script

Bash-x Script: Stepping

Types of Bash variables:

Local variables (local variables)

Environment variables

Positional variables: $, $, ... shift

Special variables:

    • $?
    • $#: Number of parameters
    • $*: Parameter list
    • [email protected]: parameter list

Shell programming in Linux (i)

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.