Different shell execution methods and their differences

Source: Internet
Author: User

Assume that the program file name is/home/dmtsai/shell. sh, which can be executed in the following ways:

1. direct command execution: The shell file must have the executable (rx) permission. Then:

(1) execute the absolute path:/home/dmtsai/shell. sh;

(2) execute relative paths. If the working directory is in/home/dmtsai/, run./shell. sh;

(3) "PATH": place the shell in the directory specified by PATH or add the directory where your shell file is located in PATH.

2. Execute the command using a bash process, and execute the command using bash shell. sh or sh shell. sh.

3. Using different execution methods will lead to different results, especially for the bash environment.

(1) When the script is executed using direct commands (either absolute or relative paths or in $ PATH) or bash (or sh, this script is actually executed in the bash of the subprogram! The point is that after the subroutine is completed, the variables in the subroutine will end and will not be uploaded back to the parent program 』! What does this mean?

For example:

[Html]
[Root @ www scripts] # vi sh02.sh
#! /Bin/bash
# Program:
# User inputs his first name and last name. Program shows his full
Name.
# History:
#2005/08/23 VBird First release
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH

Read-p "Please input your first name:" firstname # prompt for user input
Read-p "Please input your last name:" lastname # prompt for user input
Echo-e "\ nYour full name is: $ firstname $ lastname" # The result is output by the screen.

[Root @ www scripts] # vi sh02.sh
#! /Bin/bash
# Program:
# User inputs his first name and last name. Program shows his full
Name.
# History:
#2005/08/23 VBird First release
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
 
Read-p "Please input your first name:" firstname # prompt for user input
Read-p "Please input your last name:" lastname # prompt for user input
Echo-e "\ nYour full name is: $ firstname $ lastname" # The result is output by the screen.

If you run the command directly, the following result is displayed:

[Html]
[Root @ www scripts] # echo $ firstname $ lastname
<= Confirmed. The two variables do not exist!
[Root @ www scripts] # sh sh02.sh
Please input your first name: VBird <= This name is entered by laruence.
Please input your last name: Tsai

Your full name is: VBird Tsai <= see! In script operation, these two variables are:
Effective
[Root @ www scripts] # echo $ firstname $ lastname
<= In fact, these two variables do not exist in the bash of the parent program!

[Root @ www scripts] # echo $ firstname $ lastname
<= Confirmed. The two variables do not exist!
[Root @ www scripts] # sh sh02.sh
Please input your first name: VBird <= This name is entered by laruence.
Please input your last name: Tsai
 
Your full name is: VBird Tsai <= see! In script operation, these two variables are:
Effective
[Root @ www scripts] # echo $ firstname $ lastname
<= In fact, these two variables do not exist in the bash of the parent program!
 

The above results should be very strange to you. How can I use the sh02.sh-set sending volume to make it invalid in the bash environment! What's going on? If we plot the program relevance, we will explain it. When you use the direct execution method, the system will give a new bash for us to execute the commands in sh02.sh. Therefore, your firstname, lastname and other sending volumes are actually executed in the bash subroutine. After sh02.sh is executed, all data in the bash subprogram is cleared. Therefore, when echo $ firstname under the parent program is in the exercise above, nothing can be seen! Can this be understood?

 

(2) using source (or.) for execution is different. It is actually executed in the parent process. The result is as follows:

[Html]
[Root @ www scripts] # source sh02.sh
Please input your first name: VBird
Please input your last name: Tsai

Your full name is: VBird Tsai
[Root @ www scripts] # echo $ firstname $ lastname
VBird Tsai <=! Data is generated!

[Root @ www scripts] # source sh02.sh
Please input your first name: VBird
Please input your last name: Tsai
 
Your full name is: VBird Tsai
[Root @ www scripts] # echo $ firstname $ lastname
VBird Tsai <=! Data is generated!

The execution process is as follows:

 
 

 

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.