Shell Execution Script

Source: Internet
Author: User
Tags terminates

The Shell function is to explain the execution of the user's command, the user enters a command, the shell explains the execution of this article, this method is called interactive, but there is another way to execute the command is called batch mode, the user wrote a shell script, the shell can be executed at once these commands.

Here's an example:

The first method of execution: Chmode+x script.sh

Execution process:

The shell will fork a child process and call exec to execute./script.sh This program, the EXEC system call will replace the process code snippet with the./script.sh Program code snippet, exec has a mechanism, if you want to execute a text file, And the first line shebang specifies the interpreter, replaces the current process with the code snippet of the interpreter program, and executes from the _start of the interpreter, and the text file is passed to the interpreter as a command-line argument

The second mode of execution:

Shell execution Process:

1. Interactive shell (bash) fork/exec a child shell (SH) is used to execute the script, and the parent process bash waits for the child process sh to terminate.
2. SH read the CD in the script. command, call the appropriate function to execute the built- in command ( do not create the child process, run the parent process ), and change the current working directory to the top level directory .
3. SH reads the LS command in the script, fork/exec This program, lists the files under the current working directory, SH waits for LS to terminate.
4. After the LS terminates, SH continues to execute, reading to the end of the script file, SH terminates.
5. When SH terminates, bash resumes execution and the print prompt waits for user input.

The effect of the two methods of executing the shell script is the same, CD: Command changes the PWD of the child shell without affecting the interactive shell.

source./script.sh or. ./script.sh command:

The source or. command is the shell's built-in command, which does not create a child shell, but rather executes the commands in the script line-by-row under the interactive shell.

Shell Execution Script

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.