Unix Shell Scripting Programming Knowledge point

Source: Internet
Author: User
Tags exit

Scripting programming languages and compiled languages:

Scripting programming Language: (Bash)

The scripting language is usually interpreted (interpreted), which is read into the program code primarily by the interpreter (interpreter) and is translated into an internal form for execution.


can easily handle objects such as files and directories.


Run less efficiently than compiled languages

Compiled languages: (c, C + +, Java, Fortran, Ada, Pascal)

Most compiled languages work at the bottom, dealing with bytes, integers, floating-point numbers, or other objects of the machine layer.

Basic syntax format for shell scripts:

Script must begin with #!: (# Cat/etc/shells)

For example #!/bin/bash (interpreter)

# Some annotation information can be added among them, such as the use of the script, the function of the script, the date of creation, the author and other related information

Then give the script permission to execute, # chmod +x scripts.sh

Execution uses the./scripts.sh # #也可以将此脚本的路径添加到PATH变量中 and runs directly using the script name later.

Script's Test tool bash:

-N: Check the script for syntax errors, display error messages, or no information (no message is the best message)

-X: Examine the detailed procedures in the execution of the script (often used in a wrong row)

Exit: Quit script (its value is 0-255)

If the script does not explicitly define an exit code, the exit code for this script exits before the execution of the script ends.

# echo $? # #查看上一个命令执行结果所显示的状态码

Summary of the logical relationship of the shell script:

Logic and: Symbol for &&:

If one of them is false, the result must be false

If the first condition result is false, then the second condition is not to be judged, and the final result is displayed

If the first condition result is true, the second condition must determine


# useradd Redhat && echo "Redhat" | passwd--stdin Redhat

Explanation: If the useradd Redhat succeeds, proceed to the next command, or terminate the execution.

Logic or | | :

If one of the conditions results is true, the result must be true without checking for subsequent statements

If one of the criteria results is false, the next conditional statement is checked


# ID Redhat | | Useradd Redhat

Explanation: If the Redhat user exists, the Redhat user information is displayed, otherwise this account will be added.

Examples of logical and logical or federated usage:

# ID Redhat && echo "Redhat already existing" | | Useradd Redhat

Explanation: If the Redhat user exists, the Redhat user already exists, or the account is added.

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.