2016-03-10 25 views
0

具体而言,它是如何解释引号中的参数或标准输入重定向的参数(例如<)?Vala的Process.spawn_command_line_async在解释CLI参数时遵循了哪些规则?

我有以下字符串:

string cmd = "mail -s 'Work Order #%s' -c %s -r [email protected] %s < email.txt".printf(wo.get_text(), ownmail, outmail.get_text()); 

当我使用

Posix.system(cmd); 

命令运行正常和发送电子邮件,从email.txt取了身体。

当我使用

Process.spawn_command_line_async(cmd); 

我从邮件命令的错误“-c选项未找到”或类似的话。当我在工作订单#%s周围丢失了引号,而是转义空格时,电子邮件发送(包含反斜杠的主题行),而不是从email.txt获取邮件正文,它会将email.txt视为该电子邮件的另一个收件人(它显示在我的收件箱中,收件人部分的'email.txt')。 <正被忽略或丢弃。要检查的事情了,我用

Process.spawn_command_line_async("echo %s".printf(cmd)); 

这向我表明围绕主题行中的引号被丢弃,但<仍然存在。我可以在我的程序中使用Posix.system(),但为了简单起见,减少了依赖性(并且更具惯用性),我宁愿使用Process.spawn_command_line()。我错过了什么?

谢谢!

+0

从'echo'的输出将不会有,如果报价这是由shell运行的,因为shell不会因为spawn_command_line_async而删除它们。你会看到在该命令中使用'set -vx'(假设再次使用一个shell)来查看哪些参数/ etc。 shell *实际*看到。 –

回答

1

您可能想在和Shell.unquote()"".printf()参数中使用。

Vala Process.spawn_command_line_async()函数绑定到GLib的g_spawn_command_line_async()函数。因此,开始寻找更多细节的好地方是GLib文档。 GLib文档状态g_spawn_command_line_async()使用g-shell-parse-argv解析命令行。这解析了命令行,因此“只要输入中不包含任何不支持的shell扩展,就将结果定义为与从UNIX98/bin/sh获得的结果相同”。

此页还有g_shell_quote()g_shell_unquote()。这些函数被绑定到Vala,如Shell.quote()Shell.unquote()

mail只接受来自STDINg_spawn_command_line_async()的消息正文不会处理重定向。所以你要么需要一个命令行工具,将身体作为参数或者使用类似Subprocess的东西。

+0

'g_spawn_async_with_pipes()'/'Process.spawn_async_with_pipes()'处理stdin。所以你可以自己设置一个管道而不是依靠外壳。 –

0

由于双方AIThomas和Jens给我找对了方向,我能得到它用下面的代码工作:

static int main(string[] args) { 


    string subject = "-s " + Shell.quote("Work Order #123131"); 
    string cc = "-c [email protected]"; 
    string frommail = "-r " + "[email protected]"; 
    string[] argv = {"mail", subject, cc, frommail, "[email protected]"}; 
    int standard_input; 
    int child_pid; 



    Process.spawn_async_with_pipes (
     ".", 
     argv, 
     null, 
     SpawnFlags.SEARCH_PATH, 
     null, 
     out child_pid, 
     out standard_input, 
     null, 
     null); 

    FileStream instream = FileStream.fdopen(standard_input, "w"); 
    instream.write("This is what will be emailed\n".data);  

    return 0; 

} 
相关问题