Eval in a shell script

Source: Internet
Author: User

The role of Eval is to perform command-line processing again, that is, to perform two command-line processing on a single command line.

1. Example 1: Using the eval technique to implement the shell's control structure for

Use the Eval technique to implement the shell's control structure for.

[email protected] root]# cat MYSCRIPT1
#!/bin/sh
Evalit () {
if [$cnt = 1];then
eval [email protected]
Return
Else
Let Cnt=cnt-1
evalit [email protected]
Fi
eval [email protected]
}
Cnt=$1
echo $cnt | Egrep "^[1-9][0-9]*$" >/dev/null
If [$?-eq 0]; Then
Shift
evalit [email protected]
Else
echo ' ERROR!!! Check your input! '
Fi
[Email protected] root]#/MYSCRIPT1 3 hostname
Home
Home
Home
[Email protected] root]#/myscript1 5 ID |cut-f1-d "
Uid=0 (Root)
Uid=0 (Root)
Uid=0 (Root)
Uid=0 (Root)
Uid=0 (Root)


Note: There are two very special variables in bash that hold a list of parameters.

$*, which holds the string group separated by the delimiter specified by $ifs.
[Email protected], the original parameter list is saved, that is, "$" "$" ...

Here I use function recursion and eval to implement the for structure.
When eval [email protected] is executed, it goes through the following steps:
1th step, split into eval [email protected]
6th step, expand [email protected] to hostname
11th step, find the built-in command eval
Repeat command-line processing, step 11th, find the hostname command, execute.

Note: Perhaps some people want to think of course, why use eval? direct [email protected] to carry out the command is OK.

Example 2: an example of a typical error

Error! Here is a typical example for everyone to look at.

[[email protected] root]# a="id | cut -f1 -d‘‘"
[[email protected] root]# $a
id:无效选项 -- f
请尝试执行‘id --help’来获取更多信息。
[[email protected] root]# eval $a
uid=0(root)


If the command line is complex (including pipes or other characters), executing the contents of a $ A string directly will cause an error. The analysis is as follows.
The processing of $a is located in the 6th step-parameter extension, that is, skipping the pipeline analysis, so "|", "cut", "-f1", "-D" has become the parameters of the ID command, of course, error.
But using eval, it took the "id", "|", "cut", "-f1", "D" strings that were processed by the command line for the first time, and once again the command line was processed, and the pipeline was analyzed correctly.

In Conclusion: to ensure that your command or script design is handled correctly by the command line, skipping any step can result in unexpected errors!

Example 3: Setting the system's LS color display

eval $(dircolors -b/etc/dircolors)


The eval statement notifies the shell to accept the eval parameter and run them again through all the steps that the command line processes.
It allows you to write scripts to arbitrarily create command strings and then pass them to the shell for execution;
$ () is the command substitution, which returns the output string of the command.
where the dircolors command generates a bash code that sets the environment variable ls_colors according to the/etc/dircolors configuration file, as follows

[[email protected] root]# dircolors -b> tmp
[[email protected] root]# cat tmp
LS_COLORS=‘no=00:fi=00:di=01;34:ln=01; ......
export LS_COLORS
#这里我没有指定配置文件,所以dircolors按预置数据库生成代码。
其输出被eval命令传递给shell执行。


Eval is a flexible application of the Bash shell command-line processing rules to construct "smart" commands for complex functions.
the above-mentioned command is an eval of one of the most common applications, which repeats 1 command-line parameter passing procedures and executes commands purely.
In fact, it is bash's difficult point, is the Advanced Bash Programmer's compulsory skill.


Eval in a shell script

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.