Linux shell programming study notes 34: eval command

0 Preface

In the JavaScript language, there is a very special function eval. The eval function can execute a string as a JavaScript code and return an expression or value.

The built-in command eval is also provided in the Linux Shell. Does it have the function of the eval function in the JavaScript language?

1 Format, function and return value of eval command

We can use the help eval command to view the help information of the eval command.

purpleEndurer @ bash ~ $ help eval
eval: eval [arg ...]
    Execute arguments as a shell command.
    
    Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.
    
    Exit Status:
    Returns exit status of command or success if command is null.

1.1 Format of eval command

eval [parameters...]

Parameter description: There is no limit to the number of parameters, and they are separated by semicolons.

1.2 Functions of eval command

Execute the arguments as a shell command.

Specifically, the arguments are combined into a string, the result is used as input to the shell, and the resulting command is executed.

1.3 Return value of eval command

  • If there are no parameters, eval returns success (0).
  • If there are parameters, eval returns the parameters as the exit status after the command is executed.

2 eval command usage examples

2.1 eval takes no parameters

purpleEndurer @ bash ~ $ eval
purpleEndurer @ bash ~ $ echo $?
0

 

 

2.2 eval simple echo

purpleEndurer @ bash ~ $ echo $0
bash
purpleEndurer @ bash ~ $ eval echo $0
bash

available command eval echo $0 sum  echo $0 . Abilities are unique.

2.3 Use the eval command to execute other commands

We assign the command echo hello to the variable c, and then use the eval command to execute this command:

purpleEndurer @ bash ~ $ c="echo hello"
purpleEndurer @ bash ~ $ eval $c
hello

2.4 Use the eval command to execute functions

We first define three functions a1, a2, a3

function a1()
{
	echo a1; #显示 a1
}

function a2()
{
	echo a2; #显示 a2
}

function a3()
{
	echo a3; #显示 a3
}

2.4.1 Use a for loop to call the eval command to execute them

purpleEndurer @ bash ~ $ function a1(){ echo a1; }; function a2(){ echo a2; }; function a3(){ echo a3; }
purpleEndurer @ bash ~ $ for i in {1..3}; do eval a${i}; done
a1
a2
a3
purpleEndurer @ bash ~ $ 

 

2.4.2 Execute the corresponding function according to the number entered by the user

purpleEndurer @ bash ~ $  function a1(){ echo a1; }; function a2(){ echo a2; }; function a3(){ echo a3; }
purpleEndurer @ bash ~ $ echo -n enter 1 or 2 or 3:; read i; eval a${i}
enter 1 or 2 or 3:1
a1
purpleEndurer @ bash ~ $ echo -n enter 1 or 2 or 3:; read i; eval a${i}
enter 1 or 2 or 3:3
a3
purpleEndurer @ bash ~ $ echo -n enter 1 or 2 or 3:; read i; eval a${i}
enter 1 or 2 or 3:4
bash: a4: command not found
purpleEndurer @ bash ~ $ 

 As long as we enter 1 or 2 or 3, we can execute a1, a2 or a3 accordingly, and an error will occur if we enter other numbers.

2.5 Get the last command line parameter in the script

We first use the cp /dev/stdin a.sh command to create the script a.sh. The content is as follows:

echo "\$$#"         
eval echo "\$$#"

purpleEndurer @ bash ~ $ cp /dev/stdin a.sh
echo "\$$#"         
eval echo "\$$#"
purpleEndurer @ bash ~ $ . a.sh 1 2 3
$3
3
purpleEndurer @ bash ~ $ 

We directly use the command echo "\$$#", and what is displayed is $3

We use eval echo "\$$#" to display the last parameter 3 correctly.

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134887399