简体   繁体   English

执行因执行名称在变量中的脚本而产生的命令

[英]Execute command that results from execution of a script whose name is in a variable

When posting this question originally, I totally misworded it, obtaining another, reasonable but different question, which was correctly answered here .当最初发布这个问题,我完全misworded它,获得另一个,合理的,但不同的问题,这是正确回答在这里

The following is the correct version of the question I originally wanted to ask.以下是我最初想问的问题的正确版本。

In one of my Bash scripts, there's a point where I have a variable SCRIPT which contains the /path/to/an/exe which, when executed, outputs a line to be executed .在我的一个 Bash 脚本中,有一个地方我有一个变量SCRIPT ,其中包含/path/to/an/exe ,当执行时,输出line to be executed

What my script ultimately needs to do, is executing that line to be executed .我的脚本最终需要做的是执行line to be executed执行的那line to be executed Therefore the last line of the script is因此脚本的最后一行是

$($SCRIPT)

so that $SCRIPT is expanded to /path/to/an/exe , and $(/path/to/an/exe) executes the executable and gives back the line to be executed , which is then executed.以便$SCRIPT扩展为/path/to/an/exe ,并且$(/path/to/an/exe)执行可执行文件并返回line to be executedline to be executed ,然后执行该line to be executed

However, running shellcheck on the script generates this error:但是,在脚本上运行shellcheck生成此错误:

In setscreens.sh line 7:
$($SCRIPT)
^--------^ SC2091: Remove surrounding $() to avoid executing output.

For more information:
  https://www.shellcheck.net/wiki/SC2091 -- Remove surrounding $() to avoid e...

Is there a way I can rewrite that $($SCRIPT) in a more appropriate way?有没有办法以更合适的方式重写$($SCRIPT) eval does not seem to be of much help here. eval在这里似乎没有多大帮助。

You can make it simple by setting the command to be executed as a positional argument in your shell and execute it from the command line您可以通过将要执行的命令设置为 shell 中的位置参数并从命令行执行它来简化它

set -- "$SCRIPT"

and now run the result that is obtained by expansion of SCRIPT , by doing below on command-line.现在通过在命令行上执行以下操作来运行通过扩展SCRIPT获得的结果。

"$@"

This works in case your output from SCRIPT contains multiple words eg custom flags that needs to be run.这适用于您的SCRIPT输出包含多个单词,例如需要运行的自定义标志。 Since this is run in your current interactive shell, ensure the command to be run is not vulnerable to code injection.由于这是在您当前的交互式 shell 中运行的,因此请确保要运行的命令不易受到代码注入的影响。 You could take one step of caution and run your command within a sub-shell, to not let your parent environment be affected by doing ( "$@" ; )您可以谨慎行事并在子 shell 中运行您的命令,以免让您的父环境受到执行( "$@" ; )

If the script outputs a shell command line to execute, the correct way to do that is:如果脚本输出要执行的 shell 命令行,则正确的方法是:

eval "$("$SCRIPT")"

$($SCRIPT) would only happen to work if the command can be completely evaluated using nothing but word splitting and pathname expansion, which is generally a rare situation. $($SCRIPT)只有在可以完全使用分词和路径名扩展来完全评估命令时才会起作用,这通常是一种罕见的情况。 If the program instead outputs eg grep "Hello World" or cmd > file.txt then you will need eval or equivalent.如果程序改为输出例如grep "Hello World"cmd > file.txt那么您将需要eval或等效的。

Or use shellcheck disable=SCnnnn to disable the warning and take the occasion to comment on the explicit intention, rather than evade the detection by cloaking behind an intermediate variable or arguments array.或者使用shellcheck disable=SCnnnn禁用警告并借此机会评论明确的意图,而不是通过隐藏在中间变量或参数数组后面来逃避检测。

#!/usr/bin/env bash

# shellcheck disable=SC2091 # Intentional execution of the output
"$("$SCRIPT")"

By disabling shellcheck with a comment, it clarifies the intent and tells the questionable code is not an error, but an informed implementation design choice.通过注释禁用 shellcheck,它阐明了意图并告诉有问题的代码不是错误,而是明智的实现设计选择。

you can do it in 2 steps你可以分两步完成

command_from_SCRIPT=$($SCRIPT)
$command_from_SCRIPT

and it's clean in shellcheck它在 shellcheck 中很干净

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM