Shell's eval

Source: Internet
Author: User

This article will explain some of the commands used in Linux and tips to help beginners
An e v A l command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables. But the variables themselves are not complicated. The e v A l command can also be used to echo simple variables, not necessarily complex variables.
The 1 eval command can also be used to echo simple variables, not necessarily complex variables
For example:
[Email protected] ~]$ Name=valuebank
[Email protected] ~]$ eval echo $NAME
Valuebank
[Email protected] ~]$ echo $NAME
Valuebank
2 executing a command containing a string
First we first create a small file named Test that contains some text in this small file. Next, assign the cat test to the variable myfile, and now we e c h o the variable to see if we can execute the above command.
[Email protected] ~]$ VI test
[email protected] ~]$ cat test
Hello World!!!
I am a Chinese boy!
Assigns the cat testf to the variable myfile
[Email protected] ~]$ myfile= "Cat test"
If we e c h o The variable, we will not be able to list the contents of T e s t file.
[Email protected] ~]$ echo $myfile
Cat Test
Let's try the E v a l command and remember that the E v a l command will scan the variable two times.
[Email protected] ~]$ eval $myfile
Hello World!!!
I am a Chinese boy!
As can be seen from the above results, the use of the E v A l command can not only displace the variable, but also to execute the corresponding command. The
A scan is performed with variable substitution, and the second scan executes the command contained in the string, cat test.
3. Commands can also be used to display the last parameter passed to the script
[email protected] ~]$ cat Test1
#!/bin/bash
echo "Total of the arguments passed $#"
echo "The process Id is $$"
echo "Last argument OS" $ (eval echo \$$#) ""
[Email protected] ~]$./test1 Value Bank test last
Total of the arguments passed 4
The process Id is 21545
Last argument OS last
In the above script, the E v A l command first puts $ $ #解析为当前s the number of arguments for H e l L, and then the last parameter on the second scan.
4. Give each value a variable name
You can give a value a variable name. Here are some explanations for this, assuming there is a file named Test2:
You want the first column in the file to be the variable name, and the second column to be the value of the variable, so you can:
[email protected] ~]$ cat Test2
Commany TQ
LANGUE 中文版
Like YES
[email protected] ~]$ cat Test3
#!/bin/bash
While read NAME VALUE
Do
Eval "${name}=${value}"
Done <test2
echo "$COMMANY $LANGUE $LIKE"
[Email protected] ~]$./test3
TQ 中文 YES

#CatETA1b2C3#Catei whileRead Name value DoEval"${name}=${value}" Done<etEcho "$a $b $c"#. EI1 2 3

Shell's eval

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.