+ Eval learning and application in Shell +

Source: Internet
Author: User
Tags builtin echo command

1. 12 steps of bash command processing;

1. Divide command lines into tokens separated by fixed metacharacters;
SPACE, TAB, NEWLINE, ; , (, ),<, >, |,&

The types of tokens include words, keywords, I/O redirections, and semicolons.

2. Check the first mark of each command to see if it is a keyword without quotation marks or backlash.

If it is an open keyword, such as the IF and other control structure start string, function, {or (, the command is actually a compound command. Shell processes the composite command internally, reads the next command, and repeats this process. If the keyword is not the starting string of a composite command (such as a keyword in the middle of a control structure such as then), a syntax error signal is given.

3. Check the first keyword of each command based on the alias list;

If a match is found, the alias definition is replaced and the first step is returned. Otherwise, step 1 is entered. This policy allows recursive aliases and keyword aliases. For example, aliasprocedure = Function

4. Execute braces extension. For example, a {B, c} is changed to AB AC.

5. If ~ It is located at the beginning of the word and replaced with $ home ~.

Use USR's main directory to replace ~ User.

6. Execute parameter (variable) Replacement for any expression starting with the symbol $;

7. Replace the expression in the form of $ (string) with a command;

Here is the nested command line processing.

8. An arithmetic expression in the form of $ (string;

9. Divide the line parameter, command, and arithmetic replacement parts into words again. This time, it uses the characters in $ ifs as delimiters rather than the metacharacters in step 1;

 

10 *,?, [/] Perform Path Extension, also known as wildcard extension;

11. query commands by command priority table (skipping aliases;

12. Run this command after setting I/O redirection and other operations.

Ii. References

1. The first 10 steps are skipped in single quotes.
2. Double quotation marks are skipped. Step 1 ~ 5. Step 9 ~ 10, that is, only 6 ~ 8 steps.

That is to say, the double quotation marks ignore the pipe characters, aliases ,~ Replace, wildcard extension, and split into words by separators.
Single quotation marks in double quotation marks do not work, but double quotation marks allow parameter replacement, command replacement, and arithmetic expression evaluate. Double quotation marks can be included in double quotation marks by adding the Escape Character "\" and escaping $ ,',\.

Iii. Role of eval;

The role of Eval is to execute command line processing again, that is, to process a command line twice. It takes some effort to make good use of this command. Let me give you two examples.

1. Example 1: Use the eval technique to implement the shell control structure

Use the eval technique to implement the shell control structure.

 

[root@home root]# cat myscript1
#!/bin/sh
evalit(){
       if [ $cnt = 1 ];then
               eval $@
               return
       else
               let cnt=cnt-1
               evalit $@
       fi
       eval $@
}
cnt=$1
echo $cnt | egrep "^[1-9][0-9]*$" >/dev/null
if [ $? -eq 0 ]; then
       shift
       evalit $@
else
       echo 'ERROR!!! Check your input!'
fi
[root@home root]# ./myscript1 3 hostname
home
home
home
[root@home root]# ./myscript1 5 id |cut -f1 -d' '
uid=0(root)
uid=0(root)
uid=0(root)
uid=0(root)
uid=0(root)

Note:Bash has two special variables that save the parameter list.

 

$ * Stores the string groups separated by the delimiter specified by $ ifs.
$ @, The parameter list is saved as is, that is, "$1" "$2 "...

Here I use function recursion and eval to implement the for structure.
When eval $ @ is executed, it goes through the following steps:
Step 2, split into eval $ @
Step 2: extend $ @ to hostname
Step 2: Find the built-in command eval
Repeat command line processing. In Step 1, locate the hostname command and execute it.

Note:Some people may take it for granted that why Eval is used? Run the command directly at $.

Example 2: A Typical error example

Error! Here is a typical example.

 

[Root @ Home root] # A = "id | cut-F1-D ''"
[Root @ Home root] # $
ID: Invalid option -- F
Run 'id -- help' to obtain more information.
[Root @ Home root] # eval $
Uid = 0 (Root)

If the command line is complex (including pipelines or other characters), an error occurs when you directly execute the $ a string. The analysis is as follows.
$ A is processed in step 1 -- parameter extension, that is, Pipeline Analysis is skipped, so "|", "cut", "-F1 ", "-d" is changed to the parameter of the id command. Of course, an error occurs.
However, Eval is used to process the obtained "ID", "|", "cut", "-F1 ", "-d" these strings are processed by the command line again. This time, the pipeline is analyzed correctly.

 

All in all:Make sure that your command or script design can be correctly handled through the command line. Skipping any step may cause unexpected errors!

Example 3: Set the LS color display of the system

 

eval $(dircolors -b/etc/dircolors)

The eval statement notifies shell to accept eval parameters and run them again through all the steps processed by the command line.
It allows you to write scripts to create command strings at will, and then pass them to shell for execution;
$ () Is the output string of the command to be returned.
The dircolors command generates the bash code for setting the environment variable ls_colors according to the/etc/dircolors configuration file. The content is as follows:[Root @ localhost root] # dircolors-B> TMP
[Root @ localhost root] # Cat TMP
Ls_colors = 'no = 00: fi = 00: di = 01; 34: Ln = 01 ;......
Export ls_colors
# The configuration file is not specified here, So dircolors generates code based on the preset database.
Its output is passed to shell by the eval command.

Eval is a flexible application of BASH Shell Command Line Processing rules, and then constructs "intelligent" commands to implement complex functions.
The command mentioned above is a very common application of eval. It repeats the command line parameter passing process and purely executes the command.
In fact, it is a challenge of Bash and a required skill for senior bash programmers.

 

Iv. command priority table
1. Alias
2. Keywords
3. Functions
4. built-in commands
5. script or executable program ($ PATH)


5. In view of some puzzles I may encounter during my studies, I will give some interesting commands.

1. Command builtinenable

As mentioned in the above command line, step 1 will perform command search. What is the specific process of the command line?
Its default search order is function, internal commands, scripts, and executable code. We often need to skip some search items in actual programming to meet certain functional requirements. At this time, we need to use these three commands to use magic ~~

2. Command

Skip the search for aliases and functions. In other words, it only looks for internal commands and scripts or executable programs found in the search path.
Here is an interesting example.

 

[root@home root]# type -all pwd
pwd is a shell builtin
pwd is /bin/pwd
[root@home root]# cat myscript2
#!/bin/sh
pwd(){
       echo "This is the current directory."
       command pwd
}
pwd
[root@home root]# ./myscript2
This is the current directory.
/root

 

I replaced the built-in command PWD and external command/bin/pwd with the PWD () function, and then executed the built-in command PWD in the script. Why should we use command here? To avoid recursive loops, because the function name has the same name as the built-in command, and the priority of the function is higher than that of the built-in command.

3. builtin

As the name suggests, it only looks for built-in commands. This command is very simple.

4. Enable

Unlike builtin, It shields a built-in command and allows you to run a shell script or executable code with the same name without providing a full path name.
For example.

The PWD command has two built-in commands: shell and executable.

After some strange path names are executed, the shell's built-in PWD prints an "error message", but the external PWD prints the "original face" of the current directory ". See the following:

[root@home root]# cd //
[root@home //]# pwd
//
[root@home //]# type -all pwd
pwd is a shell builtin
pwd is /bin/pwd
[root@home //]# /bin/pwd
/
[root@home //]# enable -n pwd
[root@home //]# pwd
/

 

In this way, after you use enable-N to block the built-in PWD command, you can use the external PWD to print the correct path name.

Bash is profound and profound. I hope you can learn it well. :)

6. About this article

This article is published by home_king ghost in linuxsir. [bas command line processing], a topic in the org discussion area. I read this document and wrote it very well. It is suitable for beginners, the paragraphs are formatted and formatted to facilitate reading;

From: http://www.linuxsir.org/main? Q = node/134

1. Eval command-line

Command-line is a common command line typed on the terminal.However, when Eval is put in front of it, the result is that the shell scans it twice before executing the command line.For example:

Pipe = "|"

Eval ls $ pipe WC-l

When shell scans the command line 1st times, it replaces the pipe value |, and then eval enables it to scan the command line again. At this time, shell uses | as the pipeline symbol.

You can use eval if the variable contains any characters that require shell to be seen directly in the command line (not the replacement result. Command line terminator (; | &), I/O redirection operator (<
>) And quotation marks are symbols of special significance to shell, and must appear directly in the command line.

2. Eval echo \ $ #
Get the last parameter

For example, cat last

Eval echo \ $ #

./Last one two three four

Four

After the first scan, shell removes the backslash. When shell scans the row again, it replaces the value of $4 and runs the echo command.

3. Use the eval command to create a "Pointer" to the variable ":

X = 100

Ptrx = x

Eval echo \ $ ptrx
Point to ptrx. Here we can understand the example in B.

100
Print 100

Eval $ ptrx = 50
Save 50 to the variable pointed to by ptrx.

Echo $ x

50
Print 50


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.