Different shell execution methods and their differences
Source: Internet
Author: User
Different shell execution methods and their differences assume that the program file name already written is/home/dmtsai/shell. sh, which can be executed in the following ways: 1. run commands directly: the shell file must have the executable (rx) permission. Then: (1) execute the absolute path:/home/dmtsai/sh... different shell execution methods and their differences assume that the program file name already written is/home/dmtsai/shell. sh, which can be executed in the following ways: 1. run commands directly: the shell file must have the executable (rx) permission. Then: (1) run the absolute path:/home/dmtsai/shell. sh; (2) execute relative paths. assume that the working directory is in/home/dmtsai. /shell. sh execution; (3) variable "PATH" function: Place 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 the user to enter read-p "Please input your last name: "lastname # prompt the user to enter echo-e" \ nYour full name is: $ firstname $ lastname "# The result is output on the screen. if you directly run the command, the following result [html] [root @ www scripts] # echo $ firstname $ lastname <= confirm. the two variables do not exist! [Root @ www scripts] # sh sh02.sh Please input your first name: VBird <= This name is the Please input your last name: Tsai Your full name is: VBird Tsai <= See It! In script operation, these two variables take effect [root @ www scripts] # echo $ firstname $ lastname <= In fact, these two variables still do not exist in the bash of the parent program! The above results may seem strange to you? A's set sending volume with sh02.sh is 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. Results: [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! The execution process is as follows:
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.