Shell script Learning

Source: Internet
Author: User

Shell script Learning

Shell script Learning

Shell is a command parser that converts user-input commands into programs that can be run on the corresponding machine.

Shell is a program used as an interface between the user and the linux system. It allows the user to input the command to be executed to the operating system. There are multiple shells in linux.

I. Shell types:

Ash: shell developed by Bell lab. bsh is a symbolic link to ash.

Bash: it is the GNU Bourne Again shell, which is the default shell on the GNU operating system. Sh and bash2 are symbolic links to it.

Tcsh: it is a Berkeley unix c shell. Csh is a symbolic link to it.

Ii. shell commands

The basic format of shell commands is:

Command name [Option] <parameter 1> <parameter 2> ......

Shell prompt: # $

Bash in GNU tool, installed as/bin/sh by default

In most linux distributions, the shell program/bin/sh is actually a link to the Program/bin/bash.

Command/bin/sh-version can be used to view the bash version.

Iii. shell script

A Shell script is a text file that contains a series of command sequences. When you run this script file, the command sequence contained in the file will be executed.

The first line of the Shell script must be in the following format:

#! /Bin/sh

Symbol #! Specifies the parsing program of the script file. In the above example, use/bin/sh to parse the script. After the script is edited, if you want to execute the script, you must also make it executable: chmod + x filename.

During shell programming, a sentence starting with # represents a comment until the end of this line. If the annotation is used, even if the script is not used for a long time, the role and working principle of the script can be understood in a short time.

Shell script execution: sh hello. sh (assume there is a hello. sh script ).

Variable: in shell programming, all variables are composed of strings and do not need to be declared in advance,

#! /Bin/sh

# Set variable

A = 8

# Print

Echo "a is :"

Echo $

Note:

Example 1:

A = 8

What is echo $ ABC output? Why?

A = 8

What is echo $ {A} BC output? Why?

Example 2:

A = 8

What is echo "$ A" output? Why?

A = 8

Echo '$ a' output? Why?

"$ A" will be parsed as A variable and Output 8, '$ a' output $;

Default variables:

$ #: Number of command line parameters for input scripts

$ *: All command line parameter values with spaces between each parameter value

$0: Command itself (shell file name)

$1: The First Command Line Parameter

$2: The second command line parameter

Local variables:

Add the local keyword when the variable is assigned a value for the first time.

1. When a variable is assigned a value, no space is allowed between the left and right sides of "= ".

2. The statement in BASH does not end with a semicolon

IF statement

If [expression]

Then

# Code block

Fi

If [expression]

Then

# Code block

Else

# Code block
Fi

If [expression]

Then

# Code block

Elif [expression]

Then

# Code block

Else

# Code block

Fi

If [expression]; then

# Code block

Elif [expression]; then

# Code block

Else

# Code block

Fi

Comparison:

Comparison operation Integer Operation string operation

Same-eq =

Different-ne! =

Greater than-gt>

Less than-lt <

Greater than or equal to-ge

Less than or equal to-le

Blank-z

Not empty-n

Compare whether integers a and B are equal: if [$ a = $ B] (eq is also available)

Judge whether integer a is greater than integer B: if [$ a-gt $ B]

Compare whether the strings a and B are equal: if [$ a = $ B]

Judge whether string a is null: if [-z $ a]

Judge whether integer a is greater than B: if [$ a-gt $ B]

1. There are spaces left at the left and right of the "[" and "]" Symbols

2. There are spaces around "="

-E file already exists

-F files are common files.

-S file size is not zero

-D file is a directory

-The r file can be read by the current user.

-W files can be written to the current user

-The x file can be executed on the current user.

For example, S5:

#! /Bin/sh

Folder =/home

[-R "$ folder"] & echo "can read $ folder"

[-F "$ folder"] | echo "this is not file"

FOR Loop

The for loop structure is different from the C language. In BASH, the basic structure of the for Loop is:

For var in [list]

Do

# Code block

Done

Among them, $ var is the cyclic control variable, and [list] is a set that var needs to traverse. The do/done pair contains the cyclic body, which is equivalent to a pair of braces in C. In addition, if do and for are written in the same line, you must add ";" before do. For example:

For $ var in [list]; do... done

#! /Bin/bash

For day in Sun Mon Tue Wed Thu Fri Sat

Do

Echo $ day

Done

If the list is contained in a pair of double quotation marks, it is considered an element, for example:

#! /Bin/bash

For day in "Sun Mon Tue Wed Thu Fri Sat"

Do echo $ day done

In the above example, in the row where for is located, the variable day does not contain the "$" symbol, but in the loop body, the line variable $ day in which echo is located must contain the "$" symbol.

WHILE Loop

The basic structure of the while loop is:

While [condition]

Do

# Code block

Done

UNTILL Loop

The basic structure of the until loop is:

Until [condition]

Do

# Code block

Done

The difference between while and until is that when while is true, until is false.

CASE statement

The case structure in BASH is similar to the switch statement in C, and can be used for multiple branch control.

Case "$ var" in

Condition1)

;;

Condition2)

;;

*)

Default statments ;;

Esac

Example:

#! /Bin/bash

Echo "Hit a key, then hit return."

Read Keypress

Case "$ Keypress" in

[[: Upper:]) echo "Uppercase letter ";;

[[: Lower:]) echo "Lowercase letter ";;

[0-9]) echo "Digit ";;

*) Echo "Punctuation, whitespace, or other ";;

Esac

Bash shell functions

1. Command memory

2. Use the Tab key to complete commands and files

3. Set the alias of the command (eg: alias lm = 'LS-l ')

4. Job control, foreground, and background control

5. script (shell script)

6. wildcard characters are supported.

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.