Shell Programming Basics

Source: Internet
Author: User

1 Create Shell script File

To create a shell script file, you must specify the shell you want to use on the first line in the following format:

#! /bin/bash

Then add a comment to the shell file that explains what the script file is used for, who created it, when it was created, as follows:

#! /bin/functiondatewho indate-07 -date who

Save the above content as 01.shand store it in the following directory:

/root/shell

Execution 01, appears Bash:01:command not found, This is because the shell can not find the location of 01.sh;

Execute./01, appear bash:./01.sh:permission denied, This is because the current user does not have Execute permissions on 01.sh, runs Ls–l 01.sh, gets-rw-r--r--. 1 root root 00:22 01.sh. Then we need to change the current user's permissions for 01.sh: chmod u+x 01.sh, then execute./01.sh:

2 echo the Use

echo is used to output information to the display, and by default it is not necessary to enclose the displayed text in quotation marks, such as:

However, if quotation marks are used in the text that is about to be displayed, you need to enclose the entire text in another quotation mark, such as the following:

3 Use of Variables

3.1 Environment Variables

Environment variables are used to record system-related information, and you can use the SET command to display a complete list of environment variables:

To use the environment variable VARIABLE, simply use the following in the shell script file: "${variable}".

3.2 User Variables

The user variable name uses a string of letters, numbers, underscores, and no more than 20 characters, and the name is case-sensitive. The variable definition format is as follows:

Name= "Benxintuzi"

id=0001

Note: The variable is assigned a value of equal sign = Both ends cannot have spaces , as follows:

" Benxintuzi " Echo My name is: ${name}.

Shell scripts can automatically determine variable types based on variable values.

3.3 Anti-Quote

This is a special symbol that is rarely used outside of a shell script, and is a symbol on the same key. The function is to assign the result of the shell command execution to a variable, as follows:

4 Input/ Output Redirection

4.1 Output Redirection

The shell command execution results are output to a file, and the bash shell is completed in > with the following format:

Command > OutputFile

However, > overwrites the data already in the file, and you can use >> to append data to the file.

4.2 Input REDIRECT

Input redirection redirects the contents of the file to the shell command, using <, such as using the WC command to calculate the number of lines, words, and bytes of a text:

Represents a total of 4 lines of dlog text, 21 words, a total of 194 bytes.

There is also a method called inline input redirection, which can be done using command-line input as a redirect input, using <<, but you must specify a flag string for input start and end. For example, using the WC to calculate the input three rows of data, using BT as the input delimiter, the format is as follows:

Command << marker

# input data

Marker

Indicates that the input data total 3 lines, altogether 8 words, occupies 53 bytes.

5 Piping

Redirects the output of the previous command to the input of the latter command, which is called a pipe connection (piping).

The pipeline uses | The format as follows:

Command1 | Command2

6 Mathematical Operations

expr ( provided by the Bourne shell)

ARG1 | ARG2

Returns ARG2 if 0 or null is present; otherwise ARG1

ARG1 & ARG2

Returns 0 if 0 or null is present, otherwise returns ARG1

ARG1 < ARG2

Returns 1 if ARG1 is less than ARG2, otherwise returns 0

ARG1 > ARG2

Returns 1 if ARG1 is greater than ARG2, otherwise returns 0

ARG1 = ARG2

Returns 1 if ARG1 equals ARG2, otherwise returns 0

ARG1! = ARG2

Returns 1 if ARG1 is not equal to ARG2, otherwise returns 0

ARG1 <= ARG2

Returns 1 if ARG1 is less than or equal to ARG2, otherwise returns 0

ARG1 >= ARG2

Returns 1 if ARG1 is greater than or equal to ARG2, otherwise returns 0

ARG1 + ARG2

Addition operation

Arg1–arg2

Subtraction operations

ARG1 * ARG2

Multiplication operations

Arg1/arg2

Division operation

ARG1% ARG2

Take the remainder operation

String:regexp

If a regexp match exists in the string, the match is returned

Match STRING REGEXP

If a regexp match exists in the string, the match is returned

substr STRING POS LENGTH

Returns a substring that starts at POS and length

Index STRING CHARS

Returns the position of chars in a string

Length STRING

Returns the length of a string

+token

Interprets token as a string, even if it is a keyword

(EXPRESSION)

Returns the value of expression

square brackets $[operation] (bash shell, for compatibility with Bourne Shell , also contains the expr command)

Like what:

var1=$[15]echo  $var 16

Note:The bash shell operator supports only integer operations.

Floating-point arithmetic

Bc

The bash calculator recognizes numbers, variables, annotations, expressions, blocks, functions, and so on.

When using the BC, press ENTER, then enter the expression, you can perform the calculation; when exiting, enter quit.

The precision of floating-point numbers is controlled by variable scale, such as setting scale=4 in the Bash calculator, then 3/4 =. 7500. The default value for the scale variable is 0.

7 Exit Script

Each command running in the shell returns exit statusafter the run finishes, which is an integer value between 0~255 . Linux uses variables ($?). ) to save the exit status code for the previous command.

Status code

Describe

0

Command completed successfully

1

Generic Unknown error

2

Misuse of SHELL commands

126

No permission to execute this command

127

No command found.

128

Invalid exit parameter

128+x

Critical error for Linux signal x

130

Command terminated with CTRL + C

255

Exit status code out of bounds

By default, the shell script returns the exit status code for the last command in the script. Of course, you can customize an exit status code to return with the Exit command, for example: Exit 5. If the exit status code specifies a value greater than 255, the shell returns after the modulo operation is performed.

Shell Programming Basics

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.