Linux 我可以使用 ECHO 来执行命令吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17674137/
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-08-07 00:16:26  来源:igfitidea点击:

Can I use ECHO to execute commands?

linuxshellprintingscriptingecho

提问by jols

I've come up with a cool script that will produce the output that I need, but it only displays on the screen, so I have to copy, then paste in the commands to get them to execute. Here's the abbreviated version of my script:

我想出了一个很酷的脚本,可以生成我需要的输出,但它只显示在屏幕上,所以我必须复制,然后粘贴命令以使其执行。这是我的脚本的缩写版本:

#!/bin/bash
runc=/etc/csf/csf.pl -d
for IP in `tail -400 iptext.txt`
do
cc=`geoiplookup $IP`
echo -e $runc $IP    $cc | grep Algeria
echo -e $runc $IP    $cc | grep Argentina
echo -e $runc $IP    $cc | grep Armenia
echo -e $runc $IP    $cc | grep Azerbaijan
echo -e $runc $IP    $cc | grep Bolivia
echo -e $runc $IP    $cc | grep Brazil
done

Okay, so it loops through the list of IP addresses in iptext.txt, then does a geoIP lookup on each, if (in this example) there are two geoIP matches in the list, let's say for Armenia and Brazil, I will see output like this to the shell window:

好的,所以它循环遍历 iptext.txt 中的 IP 地址列表,然后对每个地址进行 geoIP 查找,如果(在本例中)列表中有两个 geoIP 匹配项,比如说亚美尼亚和巴西,我会看到输出像这样到shell窗口:

/etc/csf/csf.pl -d 46.162.242.17 GeoIP Country Edition: AM, Armenia
/etc/csf/csf.pl -d 200.147.38.50 GeoIP Country Edition: BR, Brazil

This is great, but I want more than just output, I actually want the /etc/csf/csf.pl -dcommand to run, and block (just and only) the IP in the lines that match the country name, in the list.

这很棒,但我想要的不仅仅是输出,我实际上想要/etc/csf/csf.pl -d运行命令,并阻止(仅且仅)列表中与国家/地区名称匹配的行中的 IP。

I've tried various things with my limited knowledge of shell scripting, but so far, nothing seems to work. So is there some option for ECHO I am missing that would actually run the command rather than just printing out the line?

我以有限的 shell 脚本知识尝试了各种方法,但到目前为止,似乎没有任何效果。那么是否有一些我缺少的 ECHO 选项可以实际运行该命令,而不仅仅是打印出该行?

回答by hek2mgl

A simple way that won't need modification of your script would be to pipe the command's output to another bash instance. Like this:

一种不需要修改脚本的简单方法是将命令的输出通过管道传输到另一个 bash 实例。像这样:

yourscript | bash -

The -tells bash that it should read commands from stdin.

-是告诉bash,它应该从stdin读取命令。



However, if you are not searching for a quick solution, it is possible to build and execute the command dynamically as well. Like this:

但是,如果您不是在寻找快速解决方案,也可以动态构建和执行命令。像这样:

cmd="ls"
if [ "foo" != "bar" ] ; then
    cmd="$cmd -a"
then

# ... and so on

# now execute it:
$cmd

回答by Syntax

You can use evalto execute a string:

您可以使用eval来执行一个字符串:

eval $cmdstring

回答by Aaron Digulla

Use grep -Eand a pattern instead:

使用grep -E和模式代替:

$runc $IP    $cc | grep -E 'Algeria|Argentina|...'

Explanation: echowill print the command but not execute it. Just omit it to actually execute the command.

说明:echo将打印命令但不执行它。只需省略它即可实际执行命令。

And instead of running the script 5 times, use grepto search for any of the five patterns at once.

与其运行脚本 5 次,不如使用grep一次搜索五种模式中的任何一种。

And you should use

你应该使用

cc=$(geoiplookup $IP)

instead of backticks; it's safer.

而不是反引号;它更安全。

回答by Flo Doe

There is a bash builtin eval, this executes commands like they were typed in at your shell. For example within your script you can do the following:

有一个 bash 内置的 eval,它会像在你的 shell 中输入的那样执行命令。例如,在您的脚本中,您可以执行以下操作:

eval $(echo -e $runc $IP    $cc | grep Algeria)

This will process your echo in a subshell and the output will be executed like you typed it in your shell followed by enter.

这将在子 shell 中处理您的 echo,并且输出将像您在 shell 中键入它然后回车一样执行。

See below exerpt from bash manual:

请参阅下面的 bash 手册摘录:

eval [arg ...] The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.

eval [arg ...] args 被读取并连接到一个命令中。然后这个命令被shell读取并执行,它的退出状态作为eval的值返回。如果没有参数或只有空参数,则 eval 返回 0。

回答by Bobo

echo `pwd`

above command works for me.

上面的命令对我有用。

回答by Neeraj Verma

Just put your command into parenthesis like this:

只需将您的命令放入括号中,如下所示:

echo $(ls)

You can also have text before the command

您也可以在命令之前添加文本

echo "The date is $(date)"

For Example

例如

echo "Enter Text Here $(Command Here)"