PHP exec命令不适用于包含变量的终端命令 [英] PHP exec command is not working with terminal command containing variable

查看:64
本文介绍了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');

这篇关于PHP exec命令不适用于包含变量的终端命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆