Bash 脚本 - 将变量内容作为要运行的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5998066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:30:38  来源:igfitidea点击:

Bash script - variable content as a command to run

bash

提问by Barata

I have a Perl script that gives me a defined list random numbers that correspond to the lines of a file. Next I want to extract those lines from the file using sed.

我有一个 Perl 脚本,它为我提供了一个对应于文件行的已定义随机数列表。接下来我想使用sed.

#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var=$(perl test.pl test2 $count)

The variable varreturns an output like: cat last_queries.txt | sed -n '12p;500p;700p'. The problem is that I can't run this last command. I tried with $var, but the output is not correct (if I run manually the command it works fine, so no problem there). What is the correct way to do this?

可变var返回一个输出,如:cat last_queries.txt | sed -n '12p;500p;700p'。问题是我无法运行最后一个命令。我试过$var,但输出不正确(如果我手动运行命令它工作正常,所以没问题)。这样做的正确方法是什么?

P.S: Sure I could do all the work in Perl, but I'm trying to learn this way, because it could help me in other situations.

PS:当然我可以用 Perl 做所有的工作,但我正在尝试以这种方式学习,因为它可以在其他情况下帮助我。

回答by hmontoliu

You just need to do:

你只需要做:

#!/bin/bash
count=$(cat last_queries.txt | wc -l)
$(perl test.pl test2 $count)

However, if you want to call your Perl command later, and that's why you want to assign it to a variable, then:

但是,如果你想稍后调用你的 Perl 命令,这就是你想将它分配给一个变量的原因,那么:

#!/bin/bash
count=$(cat last_queries.txt | wc -l)
var="perl test.pl test2 $count" # You need double quotes to get your $count value substituted.

...stuff...

eval $var

As per Bash's help:

根据 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.

回答by August Karlstrom

Your are probably looking for eval $var.

您可能正在寻找eval $var.

回答by nhed

line=$((${RANDOM} % $(wc -l < /etc/passwd)))
sed -n "${line}p" /etc/passwd

just with your file instead.

只是用你的文件代替。

In this example I used the file /etc/password, using the special variable ${RANDOM}(about which I learned here), and the sedexpression you had, only difference is that I am using double quotes instead of single to allow the variable expansion.

在这个例子中,我使用了文件 /etc/password,使用了特殊变量${RANDOM}(我在这里学到的)和sed你所拥有的表达式,唯一的区别是我使用双引号而不是单引号来允许变量扩展。

回答by Ajedi32

In the case where you have multiple variables containing the arguments for a command you're running, and not just a single string, you should notuse eval directly, as it will fail in the following case:

在这种情况下,你必须包含参数的命令,你正在运行,而不仅仅是一个单一的字符串,你应该多变量不能EVAL直接使用,因为它会在以下情况下会失败:

function echo_arguments() {
  echo "Argument 1: "
  echo "Argument 2: "
  echo "Argument 3: "
  echo "Argument 4: "
}

# Note we are passing 3 arguments to `echo_arguments`, not 4
eval echo_arguments arg1 arg2 "Some arg"

Result:

结果:

Argument 1: arg1
Argument 2: arg2
Argument 3: Some
Argument 4: arg

Note that even though "Some arg" was passed as a single argument, evalread it as two.

请注意,即使“Some arg”作为单个参数传递,也eval应将其读为两个。

Instead, you can just use the string as the command itself:

相反,您可以将字符串用作命令本身:

# The regular bash eval works by jamming all its arguments into a string then
# evaluating the string. This function treats its arguments as individual
# arguments to be passed to the command being run.
function eval_command() {
  "$@";
}

Note the difference between the output of evaland the new eval_commandfunction:

请注意eval和新eval_command函数的输出之间的区别:

eval_command echo_arguments arg1 arg2 "Some arg"

Result:

结果:

Argument 1: arg1
Argument 2: arg2
Argument 3: Some arg
Argument 4: