Linux系统exec命令(Linux system exec command)
Linux系统exec命令(Linux system exec command)
Shell's built-in exec command will not start a new shell, but with the command to be executed by the shell to replace the current process, and will clean up the environment of the old process, and other orders will be executed no longer after the exec command.
So, if you are in a shell, an exec ls, then, after the lists the current directory, the himself out of the shell, because the process of the shell has been replaced with the implementation of the ls command only one process, perform over nature also dropped out. To avoid this influence our use, the exec command in a shell script, use the script to invoke this script, call point can use bash a.s h, (a.s h is to store the command scripts), which can build a sub for a.s h shell to carry out, after the execution to exec, the child script process has been replaced by the corresponding exec command.
The source command or ". "will not create a shell for the script, but just execute the script contained in the current shell.
One exception, however, is that when the exec command is used to manipulate the file descriptor, it does not replace the shell, and when the operation is completed, it will continue to execute the next command.
Exec 3 < & 0: this command will also point the operator 3 to the standard input.
In addition, this command can be used as an option for the find command, as shown below:
(1) in the current directory (containing subdirectories), find all TXT files and find the lines containing the string "bin"
/ - name "*.txt" - exec grep "bin" {} \;
(2) delete all TXT files in the current directory (including subdirectories)
/ -name "*.txt" -exec rm {} \;
To summarize a table:
Exec command action
Exec ls performs ls in the shell, and the ls ends without returning to the original shell
Exec < file takes the contents of file as the standard input of exec
Exec > file writes the contents of the file as standard
Exec 3 < file reads the file into fd3
The content read in so