- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试解决一个简单的黑客问题,但我被卡住了。
在命令行上执行 grep 时,我没有问题grep -i \"the[^a-z0-9]\" $filename <<< "from fairest creatures we desire increase"
结果:(不返回任何内容)grep -i \"the[^a-z0-9]\" $filename <<< "the clock struck twice"
结果:(突出显示的)时钟敲响了两次
我读了这篇文章:
Can I open bash from a popen() stream?
我试过在 grep 命令前面加上“exec bash -c”和“/bin/bash -c”但无济于事。
谁能告诉我为什么我会收到这个错误以及如何解决它?
sh: 1: 语法错误:重定向意外
f 为空
我的代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include<cmath>
using namespace std;
int main(){
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int MAX_BUFFER =2048;
string output;
char fileInput [MAX_BUFFER];
// string input(istreambuf_iterator<char>(cin), {});
string input =" From fairest creatures we desire increase ,\
That thereby beauty's rose might never die,\
But as the riper should by time decease,\
His tender heir might bear his memory:\
But thou contracted to thine own bright eyes,\
Feed'st thy light's flame with self-substantial fuel,\
Making a famine where abundance lies,\
Thy self thy foe, to thy sweet self too cruel:\
Thou that art now the world's fresh ornament,\
And only herald to the gaudy spring,\
Within thine own bud buriest thy content,\
And tender churl mak'st waste in niggarding:\
Pity the world, or else this glutton be,\
To eat the world's due, by the grave and thee.\
When forty winters shall besiege thy brow,\
And dig deep trenches in thy beauty's field,\
Thy youth's proud livery so gazed on now,\
Will be a tattered weed of small worth held:\
Then being asked, where all thy beauty lies,\
Where all the treasure of thy lusty days;\
To say within thine own deep sunken eyes,\
Were an all-eating shame, and thriftless praise.\
How much more praise deserved thy beauty's use,\
If thou couldst answer 'This fair child of mine\
Shall sum my count, and make my old excuse";
string cmd_string = "/bin/bash -c grep -i \"the[^a-z0-9]\" $filename <<< "+input;
//cout<<cmd_string.c_str();
FILE *f=popen(cmd_string.c_str(),"r");
while (!feof(f))
{
if (fgets(fileInput, MAX_BUFFER, f) != NULL)
{
cout<<" line 1"<<fileInput[0];
output+=fileInput;
}
else
{
cout<<"f is null"<<endl;
}
}
pclose(f);
cout<< output;
return 0;
}
最佳答案
处理带参数的命令行标志的正常(并且几乎通用)约定是,命令行标志本身后面的单个参数是相关联的参数。后续参数是其他标志或位置参数。bash
确实如此.如果您想使用 -c
命令行标志来执行 bash 命令,整个命令需要是 -c
的参数标志,它必须是一个参数。后续参数分配给 $0
, $1
等命令执行之前。
所以如果你输入:
bash -c echo foo
bash -c 'echo $0' foo
foo
“正如预期的那样”(如果您的期望是经过精心磨练的),但这样做是正常的:
bash -c 'echo foo'
/bin/bash -c grep -i \"the[^a-z0-9]\" ...
grep
没有任何争论;
-i
和随后的参数分配给
$0
等,但由于这些从未被命令引用,这有点毫无意义。
/bin/bash -c 'grep -i "the[^a-z0-9]" ... '
<<<"...poem..."
) 必须是
-c
的一部分。论据
/bin/bash
而不是
popen
解释的命令行的一部分.
popen
用途
sh
执行提供的命令行,很有可能是
sh
无法识别“here-string”语法;这是一个 bash 扩展。因为
<<<
不是特别在
sh
,
<<
通常被解释为here-doc,后面必须跟一个分隔符字符串。后面不能跟
<
重定向;因此出现“意外重定向”错误。当您在
<<<
后面打字时很长,您显然在解析错误的重定向之前触发了“文件名太长”错误。这只是一个猜测;我没有重现问题。
"
没有问题。在单引号字符串中,但您必须反斜杠双引号以符合 C 字符串文字语法。不幸的是,字符串文字包含单引号,其中的第一个将被解析为在
bash -c '
中关闭单引号。 .并且无法对 bash 单引号字符串中的任何内容进行反斜杠转义,甚至单引号也不行。
'\''
表示每个 ' (在 C 字符串中需要为
'\\''
)。在该序列中,第一个和最后一个 ' 关闭并重新打开单引号字符串,以及
\'
中间是一个反斜杠转义的 '.
$filename
对您的命令无效,因为 $filename
从未被定义。如果它被定义,它会产生各种错误;因为它没有定义并且扩展没有被引用,它只是从命令行中消失了,但是首先不把它放在那里会容易得多。 grep
将是单行。我不认为那是你的意思。 <<<
后跟一个“词”;如果你不在它周围加上引号,你最终只会将第一个空格之前的部分视为输入。 关于c++ - 在 ubuntu 中使用/bin/bash 运行 grep 时,popen 错误重定向意外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343591/
我正在使用java引擎来处理一些内容。 我想知道这样会占用不必要的资源吗? command = 'some command to run my jar file' p = subprocess.Pop
我正在尝试使用以下代码块将一个进程的输出作为输入发送到另一个进程: p1 = Popen(command, stdout=PIPE) p2 = Popen(dgxmcmd, stdin=p1.stdo
popen 缓冲输出而系统没有。这是唯一的区别吗? 我知道 popen 和 system 都通过 shell 运行命令。但是,popen() 是否为 evil作为系统()? 最佳答案 看,从本质上讲,
用代码最容易解释: require 'timeout' puts "this block will properly kill the sleep after a second" IO.popen("
似乎既执行子进程又创建管道进行输入/输出,只是 subprocess 较新。 我的问题是,有没有subprocess.Popen可以做而os.popen不能做的功能,所以我们需要新模块subproce
我有一个生成以下输出的程序: ┌───────────────────────┐ │10 day weather forecast│
我正在使用以下命令来运行 shell 命令(创建子进程): cmd = "ls" process = subprocess.Popen(cmd, shell=True, stdout=subproce
得到结果后,我需要停止通过Python中的Popen发出的服务(在另一个线程的后台运行),但是以下方法失败了(只是使用ping)解释): class sample(threading.Thread):
Python - os.popen 和 subprocess.Popen 有什么区别? 最佳答案 os 进程功能被认为已过时。 subprocess 模块是在 Python 2.4 中引入的,作为与子
根据 python 文档 http://docs.python.org/library/subprocess.html ,建议将 os.popen 替换为 Popen 类,现在我有以下命令: impo
非常具体的问题(我希望):以下三个代码有什么区别? (我希望它只是第一个不等待子进程完成,而第二个和第三个会这样做。但我需要确定这是 only 的区别...) 我也欢迎其他评论/建议(尽管我已经很清楚
我有以下代码: pwd = '/home/user/svnexport/Repo/' updateSVN = "svn up " + pwd cmd = os.popen(updateSVN) get
我正在尝试编写简单的 c 函数,这将使我有可能看到从一个流到另一个流的数据传输进度,并将其显示在我的字符 LCD 上。 我设法传输数据并指示进度,但如何获得管道的结果? 所以基本上我想在 c 中做对应
我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/使用编译后的 win32 二进制文件,我有我的
我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell . 我这样试过: def
在 Linux 操作系统上,下面的 python 代码提供了当前目录中的目录。 dirs = os.popen('ls -d */').read().split(os.linesep) print d
当我们从 Python 2.7.3 升级到 Python 2.7.5 时,大量使用 subprocess.Popen() 的内部库的自动化测试开始失败。该库用于线程环境。调试问题后,我能够创建一个简短
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。 我想执行 obex_test蓝牙模式命令而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。这是我的代码,我写了这个
在我的 Lua 程序中,我必须捕获来自外部程序的输出。这个外部程序需要某些环境变量。所以我这样做: e = "" e = e .. "A=100;" e = e .. "B=Hi;" e = e ..
我是一名优秀的程序员,十分优秀!