Tag: Shell variable command executes the variable as a command
The shell will execute the variable when the command executes "multiple commands simultaneously execute the problem"
Today, a shell script is written that requires variables to be run as shell commands, usually in a variety of ways, such as:
' ${var} ' executes the variable contents when the shell command line
$ (${var}) variable contents when the shell command executes
${var} "Do not recommend this method" when the shell command executes the variable contents
However, these 3 methods can only run a single command, which is problematic when the command is of the following type:
Var1= ' cd/home; echo "Hello"; echo "good"
Var2= ' cd/home && echo ' Hello ' && echo ' good '
Execution at this time
$ (${var1}) will find no output, through the trace command execution process found that the shell automatically will; && use ' to limit, to solve this problem to find a lot of information, there is no good solution,
Usually our crontab command will be written in this way, and there is no problem with execution, by looking at the data of crontab and discovering the system function used, so we finally use Awk's system function to solve the problem.
Workaround:
Var1= ' cd/home; echo "Hello"; echo "good"
Var2= ' cd/home && echo ' Hello ' && echo ' good '
Echo ${var1}|awk ' {Run=$0;system (run)} '
Echo ${var2}|awk ' {Run=$0;system (run)} '
Correct execution results
Hello
Good
Hello
Good
This article is from the "Cool bit Linux" blog, so be sure to keep this source http://coolbyte.blog.51cto.com/8289854/1690549
The shell will execute the variable when the command executes "multiple commands simultaneously execute the problem"