Shell script entry, shell script

Source: Internet
Author: User

Shell script entry, shell script

There are two ways to use Shell script:

① Interactive: each time a user inputs a command, the Shell immediately executes the command;

② Batch: You can compile a complete Shell script to execute many commands in the script at one time.

View the available shell (/etc/shells file) of the system)

In Linux, the default command line terminal interpreter is Bash.

Shell script file format:

How to execute the script file test1.sh:

Mathematical operation in bash shell: Calculate the mathematical formula [operation] in square brackets and assign the result value to result = $ [operation].

Mathematical floating point solution: Use the bc command (bash built-in calculator ).

Option

Function

-Q

Do not display the bc calculator's welcome information

Enter quit or press ctrl + d to exit the bc calculator.

Floating point operations are controlled by the built-in Variable scale (value range). You must set this value to the number of decimal places you want to retain in the calculation result. The default value is 0.

Use the bc command in the script and replace $ () with the command to assign the output result to a variable:

The basic format is variable = $ (echo "options; expression" | bc)

Use inline input redirection <, to redirect data in the command line (especially for a large number of operations ):

The basic format is varible = $ (bc <EOF

Options

Statements

Expressions

EOF

)

The EOF string identifies the termination of Data redirected to the bc command.

The variables created in the bash calculator are valid only in the bash calculator.

Every command running in Shell uses an exit status code to tell shell that it has finished running. Exit status code: an integer between 0 and. It is sent to shell at the end of the command. Variable $? Used to save the exit status code of the last executed command. The exit status code can be used with the exit command.

Status Code

Description

0

Command ended successfully

1

General unknown error

2

Unsuitable shell commands

126

The command cannot be executed.

127

No command found

128

Invalid exit Parameter

128 + x

Critical errors related to Linux signal x

130

Command terminated by ctrl + c

255

Exit status code out of the normal range

The default variable of Shell script (the variable that accepts user parameters)

  • $0: name of the current Shell script program;
  • $ #: Number of parameters;
  • $ *: Parameter values in all locations;
  • $? : Execution return value of the previous command;
  • $1, $2, $3 ...... : Parameter value at the nth position.

Test: The criterion expression is valid. Format: test [parameter]. After the test command is executed separately, no information is displayed.

Echo $? Output the returned results after the execution of the previous command (test command). If the value is 0, the execution (Judgment) is successful.

Or use &, | to display relevant results

Operator

Function

-E file1

Test whether the file1 file exist exists)

-D file1

Test whether the file1 file is of the directory type and exists (directory)

-F file1

Test whether the file1 file is a normal file and exists (file)

-R file1

Test whether the current user has the read permission on the file1 file (read)

-W file1

Test whether the current user has the write permission on the file1 file)

-X file1

Test whether the current user has the execute permission on the file1 file (execute)

File1-nt file2

Test whether file1 is newer than file2 (newer than) (check file date)

File1-ot file2

Test whether file1 is older than file2 (older)

Operator

Function

N1-eq n2

Check whether n1 is equal to n2 (equal)

N2-ne n2

Check whether n1 is not equal to n2 (not equal)

1) n1-gt n2

Check whether n1 is greater than n2 (greater)

N1-lt n2

Check whether n1 is less than n2 (less)

N1-le n2

Check whether n1 is equal to or less than n2 (less equal)

N1-ge n2

Check whether n1 is greater than or equal to n2 (greater equal)

"=" And "!" cannot be used in Shell "! = "Compare integers (compare integers as strings)

From http://blog.csdn.net/zbw18297786698/article/details/77460786>

Operator

Function

Str1 = str2

Compare whether the content of str1 and str2 strings is the same

Str1! = Str2

Compare the content of str1 and str2 strings

-Z str1

Judge whether the str1 string length is 0 (or not defined)

-N str1

Judge whether the str1 string length is not 0

Str1> str2

Determine whether str1 is larger than str2 (compare each character of a string by bit and compare the size in alphabetical order)

Str1 <str2

Judge if str1 is smaller than str2

(Use>, <(greater than or less than signs) must be combined with escape characters/use, otherwise shell will mistakenly think of as a redirection symbol, and the subsequent string is mistakenly treated as a file name) (In a test that compares the size of a string, uppercase letters are considered to be smaller than lowercase letters (the opposite of the sort command, in the English environment, the sort Command considers that upper-case letters are greater than lower-case letters, that is, the upper-case letters are preferred in the sorting result ))

Operator

Function

Test1-a test2

Similar to &, true (and) is returned only when both test conditions are set)

Test1-o test2

Similar to |, returns true (or) if any test condition is true)

! Test1

Reverse status (reverse)

[] (Brackets): the judgment symbol. Bash shell provides another conditional test method, which works similarly with the test command and is often used in if... The then condition is in the limit formula.

  • The two ends of brackets and each component must be separated by a space character.

  • It is best to enclose the variables and constants in brackets in double quotation marks or single quotation marks.

["$ Yn" = "Y"-o "$ yn" = "y"] can also be written as ["$ yn" = "Y"] | [" $ yn "=" y "] (boolean logic can simplify possible return values to TRUE or FALSE)

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.