[英]Execute a command using popen
I have a C++ program in which I want to execute the following command: 我有一个C ++程序,要在其中执行以下命令:
cmd = "(diff <(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"}' file1)"
"<(perl -ne 's/^\\S+\\s//; if ((/aaa/ .. /bbb/) && /ccc/)"
" { print \"$_\"} file2)) ";
I get this error when I want to execute this command: 当我想执行以下命令时,出现此错误:
Search pattern not terminated at -e line 1.
I've noticed that the following commands work like this: 我注意到以下命令的工作方式如下:
cmd = "diff <(echo aa) <(echo bb)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r"); // doesn't work popen(**str**.c_str(),"r")
and an example perl command containing '"' works like this: 包含'“'的perl命令示例的工作方式如下:
cmd = "perl -ne '{print \"$1\"}' file"
stream = popen(str.c_str(),"r"); // doesn't work popen(**strCall**.c_str(),"r");
but if the perl command doesn't contains '"', it works both ways: 但是,如果perl命令不包含'“',则可以同时使用两种方法:
cmd = "perl -ne '{print $1}' file"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(str.c_str(),"r"); // also works popen(**strCall**.c_str(),"r");
How can I do to use both diff and perl in the same command. 如何在同一命令中同时使用diff和perl。 I assume I have to use strCall.
我假设我必须使用strCall。 I've tried also to escape the perl cmd like this, but it doesn't work:
我也尝试过像这样逃避perl cmd,但是它不起作用:
cmd = "perl -ne '{print \\\"$1\\\"}' file" // one '/' for '/', one for "'" and one for '"'.
Also it didn't worked this, but I am however not allowed to use R("str"): 同样它没有用,但是我不允许使用R(“ str”):
cmd = R"(perl -ne '{print \"$1\"}' file)"
string strCall = "bash -c \"( " + cmd + " ) 2>&1\"";
stream = popen(strCall.c_str(),"r")
Thanks. 谢谢。
I know I am not answering your question, but a common solution once you reach this many levels of quoting is to write a simple shell script and then call that from popen. 我知道我没有回答您的问题,但是一旦达到如此多的报价水平,一种常见的解决方案是编写一个简单的Shell脚本,然后从popen中调用它。
Eg, popen("/path/diffscript.sh", "r"); 例如,popen(“ / path / diffscript.sh”,“ r”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.