运行php exec时出现shell错误
我有一个PHP脚本,它执行shell命令来查找给定两个文件之间的公共项。这是我的PHP脚本的开始:运行php exec时出现shell错误,php,shell,sh,Php,Shell,Sh,我有一个PHP脚本,它执行shell命令来查找给定两个文件之间的公共项。这是我的PHP脚本的开始: $E7Bonded_File = "/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Bonded_cust_stats.csv"; $E7Single_File = "/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Single_cust_stats.csv"; $E7Common_File = "/o
$E7Bonded_File = "/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Bonded_cust_stats.csv";
$E7Single_File = "/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Single_cust_stats.csv";
$E7Common_File = "/opt/IBM/custom/NAC_Dslam/junk/Common_tn_SingleBonded_E7_cust_stats.csv";
//only do this once, with old single/bonded filenames. This will be a list to add to the existing Common file.
exec ("comm -12 <(cut -d ',' -f2 $E7Single_File| sort) <(cut -d ',' -f2 $E7Bonded_File| sort)", $outputCommon);
我在网上查看了一下,似乎正确地使用了exec()
。我希望返回的数字存储为数组,$outputcomon
关于这个错误消息有什么想法吗
*********最新答覆***************
我的解决方案最终是马里奥和米肯/我的同事的结合
#/bin/bash
在我的php脚本顶部,以及/bin/bash-c
,如下所示:
exec(“/bin/bash-c/opt/IBM/custom/NAC_Dslam/Common_list.sh”,$outputShell)#!/bin/bash
comm -12 <(cut -d ',' -f2 /opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Single_cust_stats.csv| sort) <(cut -d ',' -f2 /opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Bonded_cust_stats.csv| sort)
#/bin/bash
comm-12此错误通常出现在不支持的非bash Shell上。此错误通常出现在不支持的非bash Shell上。可能最简单的解决方案是将其转换为服务器上的可执行脚本:
#!/bin/bash
if [[ ! -r "$1" ]] || [[ ! -r "$2" ]]; then
printf "File not found\n" >&2
exit 1
fi
comm -12 <(cut -d ',' -f2 "$1"| sort) <(cut -d ',' -f2 "$2"| sort)
始终即使您认为shell参数是安全的,也要使用这些参数进行转义。最简单的解决方案可能是将其转换为服务器上的可执行脚本:
#!/bin/bash
if [[ ! -r "$1" ]] || [[ ! -r "$2" ]]; then
printf "File not found\n" >&2
exit 1
fi
comm -12 <(cut -d ',' -f2 "$1"| sort) <(cut -d ',' -f2 "$2"| sort)
始终即使您认为shell参数是安全的,也要用它来逃避这些参数。当用a检查脚本时,它会抱怨:
comm -12 <(cut -d ',' -f2 junk/PortParameter_E7_Single_cust_stats.csv| sort) <(cut -d ',' -f2 junk/PortParameter_E7_Bonded_cust_stats.csv| sort)
^-- SC2039: In POSIX sh, process substitution is undefined.
使用检查脚本时,它会抱怨:
comm -12 <(cut -d ',' -f2 junk/PortParameter_E7_Single_cust_stats.csv| sort) <(cut -d ',' -f2 junk/PortParameter_E7_Bonded_cust_stats.csv| sort)
^-- SC2039: In POSIX sh, process substitution is undefined.
您使用哪个shell进行测试,哪个shell链接为/bin/sh
?dash不支持表达式管道AFAIK。我在命令行中输入了“which sh”,它返回了/bin/sh。我将这一行添加到我的脚本exec(“which sh”,$throwaway);var_dump(一次性);它也返回了/bin/sh。我以前在php exec中执行的命令中使用过管道。这个问题是针对comm
的吗?还是Linux?同时检查“外壳”标签的说明。也就是说,您的问题是离题的,因为它确实缺少一个。您使用哪个shell进行测试,哪个shell链接为/bin/sh
?dash不支持表达式管道AFAIK。我在命令行中输入了“which sh”,它返回了/bin/sh。我将这一行添加到我的脚本exec(“which sh”,$throwaway);var_dump(一次性);它也返回了/bin/sh。我以前在php exec中执行的命令中使用过管道。这个问题是针对comm
的吗?还是Linux?同时检查“外壳”标签的说明。这就是说,你的问题是离题的,因为它确实缺少一个。你是想在上面键入“破折号”吗?马里奥,我不清楚我需要对你的回答做什么。即使sh
是bash,它将运行在一个兼容性更强的环境中,而不是进程替换available@thatotherguy所以它只适用于真正的登录或交互式shell?//然后可能需要-l
或+o restricted
。不,在非交互式shell中可以。它只需要将bash
作为bash
调用,比如用bash-c
你的意思是在上面键入“破折号”吗?mario不清楚我需要对你在回答中说的话做什么。即使sh
是bash,它将运行在一个兼容性更强的环境中,而不是进程替换available@thatotherguy所以它只适用于真正的登录或交互式shell?//然后可能需要-l
或+o restricted
。不,在非交互式shell中可以。它只需要将bash
作为bash
调用,例如使用bash-c
我尝试了这一点,使用命令运行shell脚本也有同样的问题。这很奇怪,因为命令是在命令行中运行的。我觉得这很难相信,因为shebang告诉它在bash下运行。你把脚本标记为可执行文件了吗?是的,我做的是chmod 777。我有工作要做。我像您一样将comm部分移动到shell脚本中,顶部是bin bash,我从php脚本中运行shell脚本:exec(“/bin/bash-c/opt/IBM/custom/NAC_Dslam/Common_list.sh”,$outputShell);我还有#/bin/bash在我的php脚本的顶部。顺便说一句,shell脚本是从命令行运行的,只是在我做出这些更改之前不是从php脚本运行的。它在php脚本中的exec前面添加了/bin/bash/-c,这似乎很有帮助。我尝试了这一点,并且用命令运行shell脚本也有同样的问题。这很奇怪,因为命令是在命令行中运行的。我觉得这很难相信,因为shebang告诉它在bash下运行。你把脚本标记为可执行文件了吗?是的,我做的是chmod 777。我有工作要做。我像您一样将comm部分移动到shell脚本中,顶部是bin bash,我从php脚本中运行shell脚本:exec(“/bin/bash-c/opt/IBM/custom/NAC_Dslam/Common_list.sh”,$outputShell);我还有#/bin/bash在我的php脚本的顶部。顺便说一句,shell脚本是从命令行运行的,只是在我做出这些更改之前不是从php脚本运行的。它在php脚本中我的exec前面添加了/bin/bash/-c,这似乎很有帮助。你是说使用#作为shell脚本运行comm命令吗/usr/bin/bash。我现在正在尝试,当从我的php脚本执行时,它有相同的错误。你是说使用#作为shell脚本运行comm命令吗/usr/bin/bash。我现在正在尝试,当从我的php脚本执行时,它有相同的错误。
$E7Bonded_File = escapeshellarg("/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Bonded_cust_stats.csv");
$E7Single_File = escapeshellarg("/opt/IBM/custom/NAC_Dslam/junk/PortParameter_E7_Single_cust_stats.csv");
//only do this once, with old single/bonded filenames. This will be a list to add to the existing Common file.
exec ("/usr/local/bin/your_script.sh $E7Single_File $E7Bonded_File", $outputCommon);
comm -12 <(cut -d ',' -f2 junk/PortParameter_E7_Single_cust_stats.csv| sort) <(cut -d ',' -f2 junk/PortParameter_E7_Bonded_cust_stats.csv| sort)
^-- SC2039: In POSIX sh, process substitution is undefined.
exec('/bin/bash -c " command "', $stdOut);