Basic knowledge of shell scripts 1: compilation of basic scripts

Source: Internet
Author: User
1. Use multiple commands

To run multiple commands together, you only need to input them to the same line, separated by semicolons.

Ex: data; WHO

2. Create a script file

(1) #! /Bin/bash is the first line

(2) # Add comments.

(3) Each line ends with a carriage return.

(4) grant the execution right to the script file by chmod + x

(5) call with Path

3. Display messages

You can add an echo statement to any location where extra information needs to be displayed in the shell script.

4. Use Variables

(1) Environment Variables

(2) User Variables

Variable names are case sensitive. spaces are not allowed between variables, equal signs, and variable values. As long as it is the value corresponding to the referenced variable, it must be prefixed with the $ symbol

(3) backticks: "'"

Used to assign the output of shell commands to variables.

5. Redirect input and output:

Output: ">", additional: ">"

Input (from the file): "<"

Built-in input (from the command line): "<"

6. Pipelines

Redirects the output to another command, instead of redirecting the output to a file. Use "|"

7. Mathematical computing

(1) expr: can be used to execute mathematical equations (but not good)

(2) In bash, specify a mathematical value for a variable. You can enclose the mathematical equations with dollar signs and square brackets ($ [operation. (Simpler)

However, bash shell supports integers.Algorithm. Z shell supports floating point operations

(3) floating point calculation solution

The most common method is to use the bash built-in bash calculator (BC) in the script through pipelines)

8. Exit the script.

(1) Check the exit status

$? Used to save the exit status of the last command execution. The default execution success is 0, and the unknown error is 1.

(2) Exit the command

The exit command allows you to specify an exit state when the script ends (a number or variable is available, but the range must be 0-255 ).

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.