method One: Switch toShellthe directory where the script is located (at this time, called the working directory) executesShellScript:
Cd/data/shell
./hello.sh
The./ means to execute hello.sh under the current working directory . If you do not add ./,Bash may respond to an error message that cannot be found hello.sh. Because the current working directory (/data/shell) may not be the default search path for the executing program , that is , it is not in the contents of the environment variable PASH. the Echo $PASH command is available to view the contents of the PATH . Now the /data/shell is not in the environment variable PASH , so you must add ./To execute.
method Two: Execute the bash shell script in an absolute path :
sh/data/shell/hello.sh
method Three: Use bash or sh directly to execute the bash Shell script:
Cd/data/shell
Bash hello.sh
Or
Cd/data/shell
SH hello.sh
Note that if you do this in method three, you do not have to set the execution permissions of the shell beforehand, or even write the first line in the shell file (Specify the bash path). Because method three is performed by passing hello.sh as a parameter to the SH (bash) command. This is not hello.sh to execute itself, but is called by others to execute, so do not execute permissions. Then you don't have to specify a bash path.
method Four: execute the Bash shell script in the current shell environment :
Cd/data/shell
. hello.sh
Cd/data/shell
SOURCE hello.sh
the first three ways to execute a shell script are toopen a child shell environment in the current shell(called the parent Shell) , which is the shell script in this child execution in the shell environment. when the shell script finishes executing , the child shell environment closes and then goes back to the parent Shell . The method four is executed in the current shell .
Remote execution of general background execution
### background Execution &
[Email protected] while]#./while.sh &
[1] 111720
### transfer to foreground FG
[[email protected] while]# FG
./while.sh
# # #Ctrl +z Pause
^z
[1]+ Stopped./while.sh
### tune to background to perform BG
[Email protected] while]# BG
[1]+./while.sh &
### querying the current execution script jobs
[[email protected] while]# jobs
[1]+ Running./while.sh &
Nohup Command:
after using the & command, the job is submitted to run in the background, the current console is not occupied, but once the current console is turned off ( when exiting the account ), the job will stop running. the Nohup command can continue to run the appropriate process after you exit the account. Nohup is the meaning of not hanging (no hang up ). The general form of the command is:
Nohup
[Email protected] while]# nohup while.sh &
How shell scripts are executed