The shell will execute the variable when the command executes "multiple commands simultaneously execute the problem"

Source: Internet
Author: User

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"

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.