EXEC command related in Linux

Source: Internet
Author: User
Tags stdin

EXEC command related in Linux

Both exec and source belong to the Bash Internal command (builtins commands), where you can view all the internal command information by entering man exec or man source under bash.
The bash shell commands are divided into two categories: external commands and internal commands. External commands are implemented through system calls or independent programs, such as SED, awk, and so on. Internal

Commands are implemented by a special file format (. def), such as CD, history, exec, and so on.

Before explaining the difference between EXE and source, let's explain the concept of fork.

Fork is a system call for Linux that is used to create a subprocess (child process). A child process is a copy of the parent process, from the parent process

Get a certain resource allocation and the environment that inherits the parent process. The only difference between a child process and the parent process is the PID (process ID).

Environment variables (variables passed to child processes, hereditary is the fundamental difference between local variables and environment variables) can only be passed from the parent process to the child process in one direction. Regardless of the child process environment

How the variable changes, does not affect the environment variables of the parent process.

Shell script:

There are two ways to execute shell scripts, one is to create a new shell, and then execute the corresponding shell scripts; one is executed under the current shell, not

Then enable the other shell.

The new way to create a shell and then execute scripts is to add the following statement at the beginning of the scripts file

#!/bin/sh

The usual script file (. sh) is this usage. This method first enables the new Sub-shell (the new child process) and then executes the command under it.

Another way to do this is by the source command, which no longer produces a new shell, but executes all commands under the current shell.

Source

The source command is the point (.) Command.

Enter man source under bash and find the source command explanation where you can see the explanation "Read and execute commands from filename

Current shell environment and ... ". As you can tell, the source command is executing each command in the parameter file in the current process, not the other driver

Process (or Sub-shell).

Exec:

Enter man exec under bash and find the EXEC command interpreter where you can see "No new process is created." Such an explanation, which is to say that exec lives

No new sub-processes are generated. So what's the difference between exec and source?

The EXEC command closes the current shell process when it executes, and then switches to the subsequent command to continue execution.



1. The system calls exec to replace the original process with a new process, but the PID of the process remains the same. Therefore, it can be argued that the exec system call did not create a new

Process, but replaces the contents of the original process context. The code snippet, data segment, and stack segment of the original process are replaced by the new process.

A process consists mainly of the following aspects:

(1) A program that can be executed

(2) All data associated with the process (including variables, memory, buffers)

(3) Program context (program counter PC, save program execution location)

2. Exec is a function cluster, consisting of 6 functions, which begin with excl and EXECV.

Executes the exec system call, which is generally the case with the fork () function to create a new process and then let the process execute the EXEC call. We know that in fork () build

After the new process, the parent will share the code snippet with the child process, but the data space is separate, but the parent process can copy the contents of the data space into the child process, and

The following copy is also copied to the child process. In order to improve efficiency, a copy-on-write strategy is adopted, that is, when the child process is created, the address space of the parent process is not copied.

The parent-child process has a common address space, and the address space is replicated only when the child process needs to write data (such as writing data to the buffer), copying the buffer to the child

Process. Thus, the parent-child process has an independent address space. After executing exec for fork (), this strategy can improve the efficiency, if the first

Copy, then after exec, the child process data is discarded and replaced by the new process.

3. The difference between exec and system

(1) Exec is directly with the new process to replace the original program run, after the completion of the operation is not back to the original program.

(2) system is called by the Shell to execute your command, system=fork+exec+waitpid, after execution, go back to the original program. Continue to perform the following

Part.

In short, if you call with Exec, you should first fork a new process and then exec. The system does not need you to fork the new process, has been encapsulated.

EXEC I/O redirection detailed and application examples

1, the basic concept (this is the premise of understanding the knowledge behind, it must be understood)

A, I/O redirection is usually related to FD, the Shell's FD is usually 10, namely 0~9;

b, there are 3 commonly used FD, 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor

, monitor related;

c, used to change the sent data channel (stdout, stderr), so that it output to the specified file;

E, 0 is the same as the 1>;

F, in IO redirection, stdout and stderr pipeline will be ready before the stdin read into the data;

G, piping "|" (Pipe line): The stdout of the previous command received the stdin of the next command;

H, Tee command is in the case without affecting the original I/O, the stdout copy to the file;

I, Bash (ksh) the process of executing commands: Parse command-variable Evaluation-command substitution ("and $ ())-redirect-wildcard expansion-Determine path-execute command;

J, () put command group in Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell

Standard input, output, and error plus any other open file descriptors.

K, EXEC command: Often used to replace the current shell and restart a shell, in other words, does not start the child shell. When using this command, any current

The environment will be cleared. exec does not overwrite your current shell environment when working with file descriptors, and only then.

2. cmd &n using System Invoke DUP (2) to copy the file descriptor N and use the result as standard output

&-Turn off standard output

n&-means the n output is turned off

All of the above can be preceded by a number, at which time the file descriptor is specified by this number instead of the default of 0 or 1. Such as:

... 2>file runs a command and directs the error output (file descriptor 2) to file.

... 2>&1 runs a command and merges its standard output and output. (Strictly speaking, the file descriptor 2 is created by copying the file descriptor, but the effect

Typically, two streams are merged. )

We explain 2>&1 in detail: 2>&1 is fd2=fd1, this is not to say that the value of FD2 equals FD1 value, because > is to change the data sent out of the letter

The FD2 "Data output channel" is changed to FD1 "Data output channel". If only this, the change seems to have little effect, because

Default output for FD2 and FD1 default output are all monitor, same!

However, when FD1 is a different file, or even other FD, this has a special purpose. Please make sure you understand this.

3, if stdin, stdout, stderr was redirected or closed, but did not save the original FD, can you restore it to the default state?

If stdin is turned off because it causes an exit, it must not be recovered.

If you redirect or close one of the stdout and stderr, you can recover because they are sent to monitor by default (but do not know if there are any other effects). Such as

Resume redirection or shutdown of the stdout:exec 1>&2, restore redirected or closed stderr:exec 2>&1.

If stdout and stderr are all closed, and the original FD is not saved, you can use: Exec 1>/dev/tty recovery.

4, cmd >a 2>a and cmd >a 2>&1 why different?

CMD >a 2>a:stdout and stderr are sent directly to file A, a file is opened two times, resulting in stdout and stderr covering each other.

CMD >a 2>&1:stdout is sent directly to file A, stderr is inherited from the FD1 pipeline and is then sent to file a. A file is only opened once, that is FD1

To open it.

I think: the difference between them is:

The cmd >a 2>a is equivalent to using two channels that compete with each other to use file A;

The cmd >a 2>&1 uses only one pipe, but it already includes stdout and stderr at its source.

The efficiency of the CMD >a 2>&1 should be higher in terms of IO efficiency!

EXEC 0exec 1>outfilename # Open File Outfilename as stdout

EXEC 2>errfilename # Open file Errfilename as stderr

EXEC 0&-# Close FD1

EXEC 5>&-# Close FD5

[[email protected] shell]$ Cat 1

11 22 33) 44 55

66 22 33) 11 33

324 25 63) 634 745

[[email protected] shell]$ Cat 2

> 1.txt

EXEC 4<&1

EXEC 1> 1.txt

While Read line

Do

Echo $line

Done < 1

EXEC 1<&4

EXEC 4>&-

[[Email protected] shell]$ sh./2

[email protected] shell]$ cat 1.txt

11 22 33) 44 55

66 22 33) 11 33

324 25 63) 634 745

[Email protected] shell]$



To modify the time format of the LS display:

Ls-l--time-style ' +%y/%m/%d%h:%m:%s '
Total 0
-rw-r--r--1 root root 0 2008/08/01 12:23:06 file1
-rw-r--r--1 root root 0 2008/08/01 12:23:06 file2

--time-style can also exist in environment variables:

Export time_style= ' +%y/%m/%d%h:%m:%s '

Ls-l
Total 0
-rw-r--r--1 root root 0 2008/08/01 12:23:06 file1
-rw-r--r--1 root root 0 2008/08/01 12:23:06 file2





Internal commands and external commands in Linux:
Unix commands have internal commands and external command points. Internal commands are actually part of the shell program, which contains some of the more concise UNIX system commands that are recognized by the shell and run inside the shell, typically loaded and homed in the system memory when the UNIX system is loaded and run. External commands are part of the utilities in UNIX systems, because the utilities are generally powerful, so they contain a large number of programs that are not loaded into memory with the system when the system is loaded, but redeployment memory when needed. Entities that are normally external commands are not included in the shell, but their command execution procedures are controlled by the shell. The shell program manages the path lookup, loading and storage of external command execution, and controls the execution of the command.
After understanding internal commands and external commands, you may have questions?? So how do I see if a command is an internal command or an external command?

1, enter the man builtins under the terminal, you can bring up the outline and explanation of internal command.

2. Use the command to turn it out, the latter will print the branch internal command

[[email protected] shell]# enable LS

Bash:enable:ls:not a shell builtin

EXEC command related in Linux

Related Article

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.