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.
linux执行shell命令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 sort < & 3 fd3 is classified
Exec 4 > file will be written to the file in fd4
Ls > & 4 ls will not be displayed, directly to fd4, in the file above
Exec 5 < & 4 create fd4 copy fd5
Exec 3 < & close fd3
The exec execution program
Although exec and source are executed directly in the parent process, exec is a big difference from source, which executes shell scripts and returns the previous shell. The exec execution does not return to the previous shell, but instead, the previous login shell is viewed as a program and copied on it.
Example:
Root @ localhost: ~ / test# exec ls
Exp1 exp1 exp5 Linux - 2.6.27.54 ngis_post. Sh test xen - 3.0.1 - install
The < logout >
Root @ localhost: ~ / test# exec > text
Root @ localhost: ~ / test# ls
Root @ localhost: ~ / test# PWD
Root @ localhost: ~ / test# echo "hello!"
Root @ localhost: ~ / test# exec > / dev/tty
Root @ localhost: ~ / test# cat text
exp1
exp5
Linux - 2.6.27.54
Ngis_post. Sh
The test
The text
Xen 3.0.1 - install
/ root/test
Hello!
Root @ localhost: ~ / test#
Exec > text is the standard output that opens the current shell to the text file
Root @ localhost: ~ / test# cat test
ls
The Pwd
Root @ localhost: ~ / test# bash
Root @ localhost: ~ / test# exec < test
Root @ localhost: ~ / test# ls
Exp1 exp1 exp5 Linux - 2.6.27.54 ngis_post. Sh test text xen - 3.0.1 - install
Root @ localhost: ~ / test# PWD
/ root/test
Root @ localhost: ~ / test#
Root @localhost: ~ / test# exit # is executed automatically
2. Redirection of exec
First, let's look at it in the /dev/fd/directory:
Root @ localhost: ~ / test# CD/dev/fd
Root @ localhost: / dev/fd# ls
0 1 2 255
The default is to have these four items: 0 is the standard input, and the default is the keyboard.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论