Shell script execution has three methods: bash Script Name sh Script Name chmod + x Script Name test #. sh (extension not allowed) cdpwdechocomplete. However, during the execution, the path changed during the process. However, the final result is that the path is not changed. [fedora @ localhost ~] $ Basha. shcomplete [fed
There are three methods for executing shell scripts.
Bash Script Name sh Script NameChmod+ X Script Name
Use the following statement to test
# A. sh (the extension is optional)
Cd/
Pwd
Echo "complete"
However, during actual execution, the path is changed, but the final result is that the path is not changed.
[FEdOra @ localhost ~] $ Bash a. sh
/
Complete
[Fedora @ localhost ~] $
Cause analysis:
When the script is executed, only a sub-process is opened under the current shell. The directory switch operation is only valid for subsequent commands in the process, but cannot change the directory of the parent process.
Solution:
Method 1: Use source a. sh.
Method 2:
[Fedora @ localhost ~] $ Cd ~
[Fedora @ localhost ~] $ Chmod u + x a. sh
[Fedora @ localhost ~] $ ../A. sh
/
Complete
[Fedora @ localhost/] $
For $ ../a. sh
The first point is the bash internalCommandTo run in the current shell.
"./A. sh" is the command parameter, that is, the script to be executed.
Note: There must be spaces between two points.
A small Script: oop directory or file. If it is a directory, enter the directory. If it is a file, edit and change the file:
1. oop. sh Code #/bin/sh
# OpenFileOr directory
$1> &/dev/null
If [-d $1]; then
Cd $1;
ELsE
Vim $1;
Fi
2. the alias oop is source/tmp/oop. sh:
AliasOop = 'source/tmp/oop. Sh'
After executing oop commands in the future, folders and files will be free of will.