<linux >bash Shell Getting Started--linux learning notes

Source: Internet
Author: User
Tags arithmetic readable

First of all, I personally understand the shell, I think the shell is a kind of command through a variety of control to implement the collection of Linux commands a batch processing of a scripting language.

Shell programming is very simple, but the syntax is not much, but advanced shell programming is very difficult, because the shell is a bridge between the user and Linux, to write high-quality shell script, you need to have a very comprehensive knowledge of Linux.

Here we only analyze the syntax of the shell script, such as a thorough study of Linux still need to do more.

Shell structure

1.#! Specifies the shell that executes the script

2. #注释行

3. Command and control structure

Steps to create a shell program:

1. Create a file that contains commands and control structures

2. Modify the permissions of this file so that it can execute, using chmod u+x

3. Execute./example or SH exmaple

Here is a simple shell script that shows the current directory path, and the sub-file

demo.sh

#!/bin/sh

#we use of PWD to print the directory path

/bin/pwd

#we use LS to print the children directories

/bin/ls

Variable

There are two types of shell variables

1. Temporary variables

Only works in the current file. Includes user-defined variables and positional variables

2. Permanent variables

A permanent variable is also called an environment variable whose value does not disappear with the end of the script

When setting a variable, use the variable name to assign the value directly, such as date= ' Date ', and add $ when using the variable, such as Echo $date

/~example.sh

Date= ' Date '

Echo $date

Output: Tue 07:48:45 EDT 2014

Use the SET command to list all environment variables

#set | Less

Use unset to clear variables that have already been defined

such as: #unset date

Positional variables

AGRC and argv similar to the C language

For example:

LS file1 file2 file3

$ A file name for this program LS

$n the nth parameter of this program n >=1

Special variables

$* all the parameters of this program

$ #这个程序的参数个数

$$ the PID of this program

$! to perform the PID of the previous background command

$? Perform the return value of the previous command

Input/output operation

Read MyName//Get name from keyboard

echo $myname//Output name

Of course, you can read multiple data at once, and output multiple data

Read P1 p2 p3

echo $p 1 $ $p 3

Arithmetic operations

Expr command: Arithmetic operations on integer variables

For example: Expr 3 + 5//number and operator with a space, otherwise it will output 3+5 instead of the desired 8

Expr 5–2

Expr 5 \* 2//need to escape *

Expr 5/2

There is no parentheses in the shell operation, and if you want to specify an operation step in a polynomial, you should use expr instead of parentheses

such as expr ' expr 5–2 ' \* 2//where ' the symbol for the keyboard sits on the upper corner is not single quotes!!!!

Variable Test statement

Used to test if variables are equal, empty, file type, etc.

Format: Test condition//Test Range: Integer, string, file

String test:

Test STR1=STR2//tests whether two strings are equal

Test STR1=STR2

Test STR1//tests whether the string is not empty

Test–n str1//Ibid.

Test–z str1//test string is empty

Integer test:

Test Intl–eq Int2//testing equals

Test Int1–ge Int2//testing int1 is greater than or equal to

Test INT1–GT int 2//greater Than

Test Int1–le int//less than equals

Test Int1–lt Int2//less than

Test Int1–ne Int2//testing is not equal

File test:

Test–d File//is a directory

Test–f file//is regular

Test–x File//is an executable

Test-r File//readable

Test–w File//writable

Test–a file//files exist

Test–s file//files size is 0

Variable test statements can be simplified []

Test–d $ equivalent to [–d $]//with spaces between

Conditional control Statements

1.if

Structure:

If Test–d

Todo

Elif Condition 2 Then//else if

Todo

Fi

2.exit statements

Exit the program execution, and return a return code, return code of 0 means normal exit, not 0 indicates an abnormal exit

3. Combination of multiple conditions

-A: logical AND, the result is true only if two conditions are set

-O: Logical OR, two conditions as long as one is established, the result is true

such as [–w $file –a–r $file]//To determine whether the file is readable and writable

4.for ... done statement

The for variable in the Name table//Name table is similar to an array, except that there are no arrays in the shell, so you need to separate all the lists with spaces

Do

Command List

Done

5.select

Select variable in keyword

Do

Command List

Done

Select makes each item in a keyword similar to a form, and executes the command between do and done in an interactive manner

6.CASE...ESAC statements

Case variable in

String 1) command list 1;;

String 2) command list 2;;

Esac

7.while

While condition

Do

Command

Done

8.shift instruction

For each execution, the sequence of parameters is shifted left one position, $ #的值减1

Used to process each parameter separately, the parameters moved out are not available

Application of the function

Definition of a function

Function name ()

{

Command sequence

}

Call to function:

Function Name parameter 1 parameter 2 ...

Debugging of Scripts

Sh–x Script

Executes the script and displays the values of all variables

Sh–n Script

Executing the script only checks for syntax errors and will return all syntax errors

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.