PHP exec命令不适用于包含变量的终端命令

问题描述:

我正在使用以下php代码.

I am using following php code.

$i = "1TEN";
$val=exec('cat '$i'.dssp -n | grep " ACC " | grep "[0-9]\+" -o | head -n 1');
echo $val;

它没有给出任何错误或任何输出.但是,以下代码可以很好地工作:

It doesn't give any error or any output. However, following code works well:

$val=exec('cat 1TEN.dssp -n | grep " ACC " | grep "[0-9]\+" -o | head -n 1');
echo $val;

任何人都可以帮忙吗?

类似地,

$line=exec("tail $i.dssp -n $diff | awk -F" " -v var=$pos '{if ($2==var) print FNR}");

您收到此错误,是因为您以错误的方式在字符串中插入变量–您应使用双引号"指定您的字符串并转义"放在带有\的文本中,因此它不会被解释为字符串的结尾:

You are receiving this mistake because you are inserting variable in a string in a wrong way – you should use double quotes " to specify your string and escape " in a text with \ so it won't be interpreted as the end of a string:

$val = exec("cat {$i}.dssp -n | grep \" ACC \" | grep \"[0-9]\+\" -o | head -n 1");

使用双引号"指定您的字符串,并用单引号'替换文本中的":

or use use double quotes " to specify your string and replace " in text with single quotes':

$val = exec("cat {$i}.dssp -n | grep ' ACC ' | grep '[0-9]\+' -o | head -n 1"); 

如果要使用单引号',则可以将字符串连接起来,如下所示:

If you want to use single quotes ' you can concatenate you string like it is shown below :

$val = exec('cat ' . $i . ' .dssp -n | grep " ACC " | grep "[0-9]\+" -o | head -n 1');