Shell script: eval and EXEC command

Source: Internet
Author: User


Due to work needs, it took some time to study the two built-in special commands of eval and exec. Because not many are used, it is a bit obscure ..


1. Eval

This command is a built-in command in bashshell, which is a little more difficult than other commands. The content following the command is considered as a parameter, but the parameters are scanned twice. The first scan replaces the variables in the parameters, and the second scan
The second scan combines the following parameters as a shell command to execute the command. It is commonly used to process variables.
The following are examples:


Case 1: directly combine commands
[[Email protected] shellscripts] # eval LS-L/home/shellscripts/
Total 8
-Rwxr-XR-x 1 Root 71 Sep 9 13:29 1.sh
-Rwxr-XR-x 1 Root 83 Sep 9 13:17 test. Sh
[[Email protected] shellscripts] # ls-L/home/shellscripts/
Total 8
-Rwxr-XR-x 1 Root 71 Sep 9 13:29 1.sh
-Rwxr-XR-x 1 Root 83 Sep 9 13:17 test. Sh
[[Email protected] shellscripts] #
From the above information, we can see that when Eval is directly followed by native parameters, it is directly executed as a command. The execution result at this time is no different from the direct command execution in shell.

Case 2: replace variables
Write as follows:
[[Email protected] shellscripts] # Cat 1.sh
#! /Bin/bash
Dirpath =/home/shellscripts
Cmd = "ls-L $ dirpath | awk-F ''' {print \ $9 }'"
Eval $ cmd
[[Email protected] shellscripts] #
[[Email protected] shellscripts] #./1.sh

1. Sh
Test. Sh
[[Email protected] shellscripts] # ls-L/home/shellscripts/| awk-F ''' {print $9 }'

1. Sh
Test. Sh
[[Email protected] shellscripts] #

From the above information, we can see that the final execution result is consistent with the result of direct command execution on the command line, that is, when eval executes $ cmd, for the first time, replace the variable $ cmd with its value. For the second time, run the variable $ cmd directly in shell as a command.

Note: In the above script, I believe you should be able to note \ $9. Why should I add a conversion character (\)? If not, it will be replaced as a location variable.

Case 3: execute any variable whose value is a command combination
First look at the example:
[[Email protected] shellscripts] # Cat 1.sh
#! /Bin/bash
Dirpath =/home/shellscripts
Cmd = "ls-L $ dirpath | awk-F ''' {print \ $9 }'"
# Eval $ cmd/comment out the row
$ Cmd
[[Email protected] shellscripts] #

In the above script, the eval row is commented out and the $ cmd variable is called directly. It seems that the value of the variable CMD is a command combination in the shell and should be able to run normally. Good luck:
[[Email protected] shellscripts] #./1.sh
Ls: cannot access |: no such file or directory
Ls: cannot access awk: no such file or directory
Ls: cannot access': no such file or directory
Ls: cannot access': no such file or directory
Ls: cannot access' {print: no such file or directory
Ls: cannot access $9} ': no such file or directory
/Home/shellscripts:
Total 8
-Rwxr-XR-x 1 Root 103 Sep 9 1. sh *
-Rwxr-XR-x 1 Root 83 Sep 9 13:17 test. sh *
[[Email protected] shellscripts] #

Yes, no. An error is reported. Except for recognizing the LS-L $ dirpath part, all the content after the MPs queue operator is recognized. However, from Case 2, we can see that eval $ cmd can be executed well.

From the comparison between case 2 and case 3, we can know that in actual use, we can assign a value to a variable for any combination of commands, run this command at the desired location through eval $ variable. Imagine if you encounter a complicated command combination,
It may need to be executed in multiple places. In this case, it is very convenient to use the eval function. Of course, some people may say that a function can be used, but the function takes effect only when called, and each call must be executed in the memory again. Once the variable is assigned a value, the variable will exist in the inner layer until the end of its lifecycle for calling.


Case 4: variable replacement assignment
Example:
Follow these steps:
[[Email protected] shellscripts] # Cat 2.sh
#! /Bin/bash
X = 100
Y = x
Eval echo \ $ y
Eval $ Y = 50
Echo $ x
Eval echo \ $ y
[[Email protected] shellscripts] #

Follow the steps to view the results:
[[Email protected] shellscripts] #./2.sh
100
50
50
[[Email protected] shellscripts] #

Analysis: From the above results, three values are returned. When the script is running, assign values to the X variable, assign values to the Y variable, and then execute eval echo \ $ Y. A 100 value is returned, which can be understood here. Run eval $ Y = 50.
Then try to output the value of $ X and find that a 50 value is output. At the beginning, the value of the X variable is 100, but it turns into 50 why? The reason is in the eval $ Y = 50 sentence, which is equivalent to a variable value, $ Y is replaced with X and 50 is assigned to the X variable, so x
The result is changed to 50, and the last sentence is run eval \ $ y again to verify the result. The result is actually changed to 50...


2. Exec
Exec is also a built-in shell command. Similar to eval and source, but the difference is that the current shell process will be replaced after executing the command after exec, but not the first two .. Exec usage is a bit special, maybe programmers will
It is quite familiar. Here we will introduce three common usage methods;

Usage 1: The shell process used to separate the execution script and exit the sub-script
Main script:
[[Email protected] shellscripts] # Cat main. Sh
#! /Bin/bash
Bash/home/shellscripts/3.sh
Ls-Al
[[Email protected] shellscripts] #

Subscript:
[[Email protected] shellscripts] # Cat 3.sh
#! /Bin/bash
Ls-L/etc | head-N 5
Exec echo "this is a test"
Echo "Hello World"
[[Email protected] shellscripts] #

Run the main script and check the result:
[[Email protected] shellscripts] #./Main. Sh
Total 1252
-RW-r --. 1 Root 44 Sep 5 18:13 adjtime
-RW-r --. 1 Root 1512 January 12 2010 aliases
-RW-r --. 1 Root 12288 Sep 2 09:24 aliases. DB
Drwxr-XR-X. 2 root Root 4096 Sep 2 11: 57 alternatives
This is a test
Total 44
Drwxr-XR-x 2 root Root 4096 Sep 9 :51.
Drwxr-XR-X. 3 Root 4096 Sep 9 14:19 ..
-Rwxr-XR-x 1 Root 103 Sep 9 13:52 1.sh
-Rwxr-XR-x 1 Root 71 Sep 9 14:02 2.sh
-Rwxr-XR-x 1 Root 81 Sep 9 :51 3.sh
-Rwxr-XR-x 1 Root 92 Sep 9 :43 4.sh
-Rwxr-XR-x 1 Root 31 sep 9 15:16 5.sh
-Rwxr-XR-x 1 Root 72 Sep 9 :43 6.sh
-RW-r -- 1 Root 41 Sep 9 15:12 File
-Rwxr-XR-x 1 Root 49 Sep 9 14:32 main. Sh
-Rwxr-XR-x 1 Root 83 Sep 9 13:17 test. Sh
[[Email protected] shellscripts] #

Analysis:
From the above Script output information, we can see that the 3. Sh script is successfully called, but you will find that the last line in the 3. Sh script is not executed because there is no output content. This is because after the command execution result after exec is output, the 3. Sh step is cleared.
Open the sub-shell process, and then return to the main shell process to continue to execute the following content ..

This feature seems to be of little use, and some people say it is better to write it directly in one step. However, you may need this method to avoid interference in complex environments ..

Usage 2: sets the descriptor to redirect the content of the input file.
The script is as follows:
[[Email protected] shellscripts] # Cat 4.sh
#! /Bin/bash
Exec 3 While read-U 3 StR
Do
Echo $ Str
Done
Exec 3 <&-

Create the/home/shellscripts/file:
[[Email protected] shellscripts] # Cat File
Hello World Haha A B C
Liuxueming
Liwei
[[Email protected] shellscripts] #

Run the script and check the result:
[[Email protected] shellscripts] #./4.sh
Hello World Haha A B C
Liuxueming
Liwei
[[Email protected] shellscripts] #

From the preceding execution results, the text content is normally output and output in the order of the while loop. Here we will explain the file descriptor. Exec can be used to redirect a file descriptor and associate it with a file.
The file descriptor operation is the operation on the file, which also complies with the Linux system file descriptor definition. For example, in step 3 above, redirect the input to the/home/shellscripts/file. In this way, you can use read-U 3 to specify the description below.
Otherwise, the file cannot be added directly after the read operation. After the call is complete, you must disable the file descriptor, for example, exec 3 above <&-

Of course, another method can be used here:
While read Str
Do
Echo $ Str
Done



Usage 3: sets the descriptor to redirect the output content to a file.
Test:
[[Email protected] FD] # pwd
/Dev/FD
[[Email protected] FD] # ls
0 1 2 255
[[Email protected] FD] # exec 4>/home/shellscripts/test
[[Email protected] FD] # ls-L/etc | head-N 5> 4
[[Email protected] FD] # exec 4> &-
[[Email protected] FD] # Cat/home/shellscripts/test
Total 1252
-RW-r --. 1 Root 44 Sep 5 18:13 adjtime
-RW-r --. 1 Root 1512 January 12 2010 aliases
-RW-r --. 1 Root 12288 Sep 2 09:24 aliases. DB
Drwxr-XR-X. 2 root Root 4096 Sep 2 11: 57 alternatives

No. The contents redirected to file descriptor 4 are all output to the file:/home/shellscripts/test...


End !!!

Stupid technology ------ not afraid of you !!!


This article is from the "Stupid technology" blog, please be sure to keep this source http://mingyang.blog.51cto.com/2807508/1550281

Shell script: eval and EXEC command

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.