Execute executable file
Executing a file is a file with executable permissions, and you ll
might see the following results if you execute or command on the directory where the file is located ls -l
:
-rwxr-xr-- 1 usr users 289 Jul 29 09:15 cronmonth
It means that the owner x
of the file has executable permissions on the file.
Assuming the Nginx installation directory /usr/share/nginx/
, its executable is /usr/share/nginx/sbin/nginx
, there are two simple ways to start nginx.
Absolute path mode
That is, the full path from the root directory /
to the executable file: /usr/share/nginx/sbin/nginx
.
Relative path mode
The first is cd
to jump to the directory where the executable is located, which is executed first cd /usr/share/nginx/sbin
, then the relative path relative to the current position is executed: ./nginx
,
Similarly, if the current working path is in /usr/share/nginx/conf
, then the relative path can still be used: ../sbin/nginx
.
The advantage of using a relative path instead of an absolute path is that you don't have to hit that many words, but in some cases it's better to use absolute paths, such as when invoking other scripts in a cron script, and it's best to specify the called script with an absolute path.
Shell scripts that execute shell scripts with executable permissions
This can be done with an absolute path/relative path, as is the case with executable files.
Shell scripts with no executable permissions
Sometimes it is not necessary to execute permissions on shell scripts, but they can still be executed by invoking the shell script interpreter to interpret the execution script.
There are several ways to invoke the Shell interpreter:
sh /path/to/script
: Called with a sh
command.
bash /path/to/script
: If the system is installed with bash.
source /path/to/script
:
. /path/to/script
: Note that there is at least one space between the period and the script path.
PATH environment variable
If the current directory has an executable file abc
, the shell command line input directly abc
, and then enter, get is probably "Command not found" prompt;
and enter directly ls
, and then enter, but you can list the current directory of files and subdirectories. That's weird? The current directory clearly has abc
the executable file, but said the command can not find, the current directory has no file in the filename has ls
these two characters of the command is executed correctly!
This is PATH
the credit for environmental variables.
When the command entered does not have path information, the system is looked up by default from the PATH
directory specified by the environment variable, so the./command method is used when the path is not configured in the Bin directory.
Here is the PATH
value of the environment variable in my VPS:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
Under Linux, the PATH
delimiter between the specified paths is a colon, and :
when the system looks up a command file, it will stitch the command to the specified path, PATH
see if the path exists, and if none exist, the command cannot be found; Check whether the user has permission to execute, or if there is a command to execute, otherwise prompt "Permission denied".
For the previously mentioned ls
command, first stitching with the PATH
specified first path /usr/local/sbin/ls
, the file does not exist, continue to the next path /usr/local/bin
, get /usr/local/bin/ls
, do not exist, continue to the next, until the /bin
directory is found and execute permission, then execute the command.
For abc
, although the current directory is known as abc
an executable file, but the current directory is not PATH
in the specified directory, so the system can not be found, so that the command cannot be found.
Benefits
With the PATH
environment variables, we do not have to typed the full path of the command or to the directory where the command is located, but directly typed the command name can be more convenient.
On the line when we install Java in the system, in addition to configuring an JAVA_HOME
environment variable, will also $JAVA_HOME/bin
add PATH
variables, that's why, for convenience!
How commands are executed under Windows
Windows also has an PATH
environment variable, but its delimiter is a semicolon ;
.
However, under Windows, the system looks for commands in addition to the PATH
environment variable, but also in the current directory lookup, that is, if there is a batch file under the current directory, even if the abc.bat
current directory is not added to the PATH
environment variable, as long as the DOS path to the current directory, abc.bat
You can also execute the script by typing it directly.
Article citation: http://coderbee.net/index.php/linux/20130730/338
Command not found problem explanation