Bash Command Execution Analysis

Source: Internet
Author: User
Tags vars

In Linux, how many types of executable files are available? How does one execute a program in the bash environment? Next we will analyze them one by one.

1. Types of executable files in Linux: 1.1 binary executable files

This type of file is the most common, such as/bin/ls,/sbin/ifconfig,/bin/cat, and so on.

[Root @ notebook135 ~] # File/bin/ls/bin/cat/sbin/ifconfig

/Bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

/Bin/cat: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

/Sbin/ifconfig: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

1.2 executable script file

This type of file is commonly used by system administrators. It is usually used to bond various other programs and is also necessary in the system, such as the Command service and yum.

[Root @ notebook135 ~] # File $ (which yum) $ (which service)

/Usr/bin/yum: a/usr/bin/python script text executable

/Sbin/service: POSIX shell script text executable

Yum is a python script and service is a shell script.

Of course, you can also have other types of scripts, such as ruby, php, and awk.

1.3 system loader identification and verification of executable files

All types of executable programs are loaded and executed through a unified execve () System Call. The specific loading process varies depending on the types of executable programs. For binary files, they are directly loaded and executed. For script files, the interpreter specified in the first line of the file is loaded, the path name of the script file is used as the interpreter parameter. How does execve () differentiate various program types? In fact, it is very simple, that is, the "magic number" located at the beginning of the file ". Different types of files have different "magic numbers". For a script file, it starts #!, Binary files vary depending on the 32-bit and 64-bit types. You can study these binary files on your own.

2 bash Command Execution Process 2.1 bash built-in Command Execution Process

There are two types of commands that can be executed in the bash environment: one is the executable file (the executable file is divided into binary and script), and the other is the bash built-in command.

Common built-in Commands include echo, cd, and trap. Built-in commands are also divided into two types: one is the replacement of some external executable files with the same function, in order to improve efficiency, such as echo; the second type is essential commands that cannot be completed by executing external files, such as cd.

For internal commands, bash can directly execute its internal code to quickly eliminate the burden. It is relatively difficult to execute external executable files.

2.2 bash execution of external executable files

There are two methods for bash to execute commands. One is in interactive mode, enter the command name and press the Enter key; the other is to use the command path as a parameter to execute/bin/bash. The two methods have the same effect.

Bash first fork out a sub-process, and then: (1) bash's own process executes wait () and waits until the sub-process ends; (2) execve ("command path") in the sub-process, the rest of the work is done by the loader.

[Root @ notebook135 ~] # Strace-e trace = process/bin/ls

Execve ("/bin/ls", ["/bin/ls"], [/* 21 vars */]) = 0

2.3 three methods for running script files (1) Direct Execution

As we mentioned earlier, execve () can automatically identify the script file type and automatically load the corresponding interpreter. As follows:

[Root @ notebook135 ~] # Strace-e trace = process./test1.sh

Execve ("./test1.sh", ["./test1.sh"], [/* 21 vars */]) = 0


(2) manually specify the interpreter for execution

Of course, this script recognition process can also be completed by the user. For example, for test1.sh above, we can specify ksh93 as its interpreter, the interpreter program is executed explicitly.

[Root @ notebook135 ~] # Strace-e trace = process ksh93./test1.sh

Execve ("/bin/ksh93", ["ksh93", "./test1.sh"], [/* 21 vars */]) = 0

The interpreter specified in the first line of the script file does not work.

The two methods of executing script files have the same effect, but the process names will be different when you view them through ps. When you execute the command directly, the sub-process names are the script file names, when ksh93 test1.sh is executed, the sub-process name is ksh93.

In fact, the second method is sometimes necessary. For example, some system loaders cannot directly execute script files. For example, script files do not have executable permissions.

In the preceding example, test1.sh is a ksh script. For awk, php, python..., the process is the same. A script may look a little special, that is, bash. At this time, it will lead to bash starting a subbash process, which actually looks special, just because the sub-process name is the same as that of the parent process, essentially, it is no different from other types of script interpreters.

(3) using the bash process itself to explain the script file (do not generate sub-processes) bash also provides a special way to execute External commands, that is, using its own processes to execute, instead of creating a child process. The interpreter specified in the first line of the script file does not work. The advantage of this method is that you do not need to establish sub-processes, of course, the efficiency is high. The disadvantage is obvious because the process isolation is lost, so the script file will directly destroy the bash process, and the security is poor. For example, the following script:
Vim test1.sh

1 #! /Bin/ksh93

2 echo $

3 exit


If you run the command directly or through bash./test1.sh, all the results will print the sub-process number, then exit the sub-process and return to the interactive bash. If it is executed through ../test. sh, bash itself exits after the bash process number is printed.
This method is mainly used to set up the interactive bash environment through scripts, such as the/etc/profile file.
It should be noted that this method is different from the direct execute () in the parent process, which is called the replacement of the program body in the same process. Here, bash only regards the content of the script file as user input on the terminal.

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.