Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转义要与PHP exec()一起使用的冗长Bash命令_Php_Linux_Bash_Shell - Fatal编程技术网

转义要与PHP exec()一起使用的冗长Bash命令

转义要与PHP exec()一起使用的冗长Bash命令,php,linux,bash,shell,Php,Linux,Bash,Shell,我有一个半长的shell命令,需要在php中执行。我觉得我已经正确地转义了所有内容,但是当从php的exec()函数执行时,它不能正常工作。当我使用exec()运行命令时,它会无错误地退出,但运行不正常 我正在将包含电子邮件地址和其他管道分隔信息的列表文件与包含原始列表中要禁止的md5版本电子邮件地址的文本文件进行比较 下面是bash命令: while read line ; do email=`echo "$line" | cut -d '|' -f1` ; if [ $(E=`echo -e

我有一个半长的shell命令,需要在php中执行。我觉得我已经正确地转义了所有内容,但是当从php的exec()函数执行时,它不能正常工作。当我使用exec()运行命令时,它会无错误地退出,但运行不正常

我正在将包含电子邮件地址和其他管道分隔信息的列表文件与包含原始列表中要禁止的md5版本电子邮件地址的文本文件进行比较

下面是bash命令:

while read line ; do email=`echo "$line" | cut -d '|' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk '{print $1}'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done < /path/to/emails
/path/to/emails是一个文本文件,包含电子邮件地址列表和一些其他删除信息:

emailtokeep@domain.com|1000|1
emailtosuppress@domain.com|1000|1
在bash中执行该命令时,输出的只是抑制列表中不存在的电子邮件地址,它工作正常:

emailtokeep@domain.com|1000|1
我遇到的问题是,当我在php中使用exec()执行相同的命令时,它会输出“emails”文件中的所有行,而不会抑制抑制文件中的任何匹配项

$supCommand = 'while read line ; do email=`echo "$line" | cut -d \'|\' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk \'{print $1}\'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done < /path/to/emails' ;
exec($supCommand, $supStatus) ;
print_r($supStatus) ;
$supCommand='读取行时;do email=`echo“$line”| cut-d\'| \'-f1`;如果[$(E=`echo-en“$email”| md5sum | awk\'{print$1}\`;grep$E/path/to/suppressions | head-1;)];然后继续;否则回声线;fi完成
我用单引号将命令括起来,并在命令中转义了单引号的四个实例。如果有人有任何想法,我将不胜感激

提前感谢:)

这似乎有效:

<?php
    $bash = '
        mapfile -t md5sums < suppressions
        function md5 { printf "%s" "$1" | md5sum | cut -f1 -d" "; }
        while IFS="|" read -r email b c; do 
            this_md5=$(md5 "$email")
            for suppress in "${md5sums[@]}"; do
                [[ $this_md5 == $suppress ]] && continue 2
            done
            echo "$email|$b|$c" 
        done < emails
    ';
    $cmd = "bash -c '$bash'";
    $keep = passthru($cmd);
    print($keep);
?>

您不能在php中完成所有这些吗?
<?php
    $bash = '
        mapfile -t md5sums < suppressions
        function md5 { printf "%s" "$1" | md5sum | cut -f1 -d" "; }
        while IFS="|" read -r email b c; do 
            this_md5=$(md5 "$email")
            for suppress in "${md5sums[@]}"; do
                [[ $this_md5 == $suppress ]] && continue 2
            done
            echo "$email|$b|$c" 
        done < emails
    ';
    $cmd = "bash -c '$bash'";
    $keep = passthru($cmd);
    print($keep);
?>