Learning about single quotes, double quotes, parameter scopes, and eval in Shell

Source: Internet
Author: User

A custom net function is used to capture the IP address, netmask, and broadcast information of the network adapter from the ifconfig output result.

##ip netmask broadcastfunction net(){        name=$1        num=\$$2        /sbin/ifconfig -a | awk '{if ( $1 == "inet" && $5 == "broadcast") print "\t"$name $num}'}echo eth:net ip 2

The above function is called once and the output IP information is used as an example: (result)

L

We can see that the output results are not exactly what you want.IP address 192.168.100.10

Why ??

Next, let's talk about my error handling process (error errors)

I changed the function.

/sbin/ifconfig -a | awk '{if ( $1 == "inet" && $5 == "broadcast") print "\t"$name $num}'

Rename:

echo "/sbin/ifconfig -a | awk '{if ( $1 == "inet" && $5 == "broadcast") print "\t"$name $num}'"

The output result is:

Analysis: It is found that $1 in the awk statement is replaced with the first parameter passed in when the function is called. I think this error occurs when awk cannot distinguish the $1 field intercepted by itself from the $1 segment of the function parameter.

Later, Dr. Hua told me that the output result was simple because the awk statement was not executed, but was simply implemented by the echo statement.Weak referenceReplace it with the variable value.

Next we will introduce strong references and weak references in shell, or analyze them with instances:

a=testecho '$a'echo "$a"echo '"$a"'echo "'$a'"

Running result:

Explanation:

Single quotation marks: strong reference (all content in the quotation marks is treated as strings, such as Echo '$ A') double quotation marks: weak reference (when there is a variable, the variable will be replaced, for example, Echo "$ ")

Both single quotes and double quotes are dominated by external quotation marks (that is, the type of external quotation marks determines whether the content in the quotation marks is strongly referenced or weak reference, such as Echo '"$ "': strong reference and echo "'$ a'": weak reference)

After learning about the difference between strong reference and weak reference, let's look back at the original problem. The error analysis method is as follows:

We can see that the single quotation marks of awk are also "strongly referenced", so both $ name and $ num cannot be passed into their values, so they are empty, awk outputs the result with two $0 values.

Therefore, "strong reference" is not acceptable. Can I change the single quotation marks of awk into double quotation marks ????? Try:

The display syntax error cannot be described,

Some may ask if you can use the eval statement (for more information about how to use the eval statement, see below)

Test :()

Obviously, Eval functions cannot be used in awk.

That is to say, the logic of this function does not work, because there is no way to assign the function parameters to awk.

Let's take another function example to learn about the return value of Shell functions.

function cpuinfo(){        $1=`cat /proc/stat | grep 'cpu ' | awk '{print "$2" "$3" "$4" "$5" "$6" "$7" "$8}'`        $2=`echo $1 | awk '{print $5}'`        $3=`echo $1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`}cpuinfo Log1 idle1 cpu1echo $Log1 $idle1 $cpu1

Let's talk about the intention of my function. I want to assign the first statement of the function to the first parameter passed in, and so on.

Error detected after running

Obviously, this variable Assignment Method is incorrect. So how can we achieve our goal.

Knowledge of shell learning. First, we will introduce eval usage through a simple example, and then perform an error correction test:

It can be seen that eval can read a series of parameters and then execute them according to the characteristics of the parameters.

For eval $ V1 = $ vaaa; echo AAA: $ AAA and eval "$ V1 = $ vaaa"; echo AAA: $ AAA, the error output is as follows:


Why is this error reported. From the above Eval: If the command is changed to AAA = This is AAA, an error will certainly be reported. This is also true for weak quotes in double quotes.

Only the strong quotes in single quotes become AAA = $ vaaa

If you want to learn eval carefully, refer to: http://www.cnblogs.com/huzhiwei/archive/2012/03/14/2395956.html

Back to question

I will perform the following tests:


Assume that you only need to replace variable A in the example with the $1 passed in by the function variable through eval. Modified the function content and tested it.

The error is still found ..................................... ........................................ ........................................ ....

Finally, I decided to use another method to solve the problem of using the value assignment multiple times for the function. As follows:

The scope of variable values of the function is involved here. It is different from the C language. This can also be seen from the above example.

Detailed study can see reference article: http://blog.csdn.net/ithomer/article/details/7954577

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.