嗨,我有以下文件:
$ cat file.txt
col1 col2 col3
zz2 mm uu
pp3 yy kk
ss2 tt ll
zz3 mm uu
pp23 yy kk
ss3 tt ll
ss3 aa 44
33c 22 99我想使用awk和下面的unix命令对它进行排序。此命令首先在column1上对选项卡文件进行排序,然后在column2上对其进行排序。
$ awk 'NR==1; NR > 1 {print $0 | "sort -t\"\t\" -k1 -k2"}' file.txt而且它工作得很完美。
$ awk 'NR==1; NR > 1 {print $0 | "sort -t\"\t\" -k1 -k2"}' file.txt
col1 col2 col3
33c 22 99
pp23 yy kk
pp3 yy kk
ss2 tt ll
ss3 aa 44
ss3 tt ll
zz2 mm uu
zz3 mm uu当我想使用Php作为bash参数来调用它时,我的问题就开始了。
我的代码工作正常如果我调用
print(shell_exec(/bin/bash -c "ls -l")) 现在我想对排序命令执行同样的操作。下面是我的php代码:
$inpfile= 'file.txt';
$outfile= 'fileout.txt';
$bash = '/bin/bash ';
print(shell_exec($bash. " -c \"ls -l\"")); print"<br />";
$command = 'awk \'NR==1; NR > 1 {print $0 | "sort -t\"\t\" -k1 -k2" }\' '.$inpfile . " > " . $outfile;
print("<br>". $command); //awk 'NR==1; NR > 1 {print $0 | "sort -t\"\t\" -k1 -k2" }' file.txt > fileout.txt
shell_exec($bash. " -c \"$command\""); //Gives error because of improper quotes.
/*
$escaped = escapeshellarg($command);
$command1 = $bash. ' -c '.$escaped ;
print("<br>". $command1);
//shell_exec($command1);
*/感谢你的帮助。谢谢
发布于 2012-02-01 03:37:16
在使用shell_exec函数时,不需要使用/bin/bash。您可以像这样清理整个代码(和它在中运行得很好):
<?php
$inpfile= 'file.txt';
$outfile= 'fileout.txt';
$command = 'awk \'NR==1; NR > 1 {print $0 | "sort -t\"\t\" -k1 -k2" }\' '
. $inpfile . ' > ' . $outfile;
var_dump($command);
shell_exec("$command"); // now works fine without any error
?>发布于 2012-02-01 03:18:24
你在找escapeshellarg和escapeshellcmd吗?
https://stackoverflow.com/questions/9085470
复制相似问题