Shell script learning notes ----- command execution, script learning notes

Source: Internet
Author: User

Shell script learning notes ----- command execution, script learning notes

1. PATH variable: the main function of shell is to execute the command entered by the user. For example, after the user inputs a "ls" command, the shell will find the file corresponding to the command and execute it. Usually, shell sets an environment variable named PATH, which stores a series of directory names used to find command files, and the order in which directories appear is very important, because shell searches in order, when the first file corresponding to the command is found, execute

2. Generally, you can choose ?" To determine whether the previous command runs successfully. Generally $? If the value is 0, the previous command runs successfully. Otherwise, the command fails. If you are familiar with the C language, you will know, in fact, $? The returned value of the previous main function is saved. Note that $? Only save the address of the previous directly adjacent command, the second $? The value is 0 because the previous "echo $? "The command is successfully executed. Usually $? As a condition for the if statement in the script, different branches are made based on the execution of the preceding command.

3. Run the command in the Background: As shown in, add the symbol '&' After the command is executed, so that the command can be executed in the background. In the second row, the number in brackets indicates the number of processes running on the background of the current shell, and the second number indicates the system process number. The essence of background running is that the shell can accept new commands without waiting for the execution of the current command to complete, but the output of background processes will still be displayed on the screen. Tips: 1) If you want to kill the following background processes, you can use kill % 1 or kill 3526. 2) If you want to re-use shell to control background processes, use the command fg % 1. 3) If you want to convert the currently running command to running in the background, you can use ctrl Z to stop the command and then enter bg to run it in the background.

4. In shell, logical operations are judged in the same way as in C. For example, the symbol "&" indicates the logic and, and executes Statement A & B. if and only when A runs successfully, it runs B. For logical or likewise available Statements, Statement A | B runs B only when A fails to run.

5. If the "set-e" command is executed and the subsequent statement fails, the entire script is exited, as shown in. If the mytmp directory does not exist in the current directory, after the cd statement is executed, the script is automatically exited and the ls statement is not run.

6. A process running in the background is usually a sub-process of shell. Therefore, when you exit shell, it sends a SIGHUP signal to its sub-process to kill it. If the process running in the background does not end with the shell, add nohup before the command, as shown in. Therefore, the shell sub-process ignores the SIGHUP signal and runs independently from the shell. (It is said that after shell is closed, a nohup. out file is automatically generated in the current directory to save the input of the sub-process. After the experiment, it is found that the file exists, but there is no content .)

The running status of the bg process before and after the shell is disabled.

 

References: bash Cookbook, Carl Albing

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.