使用php exec启动带有大括号扩展的linux命令
如何强制php exec()解释linux大括号扩展 我遇到了一个奇怪的行为,没有找到一个方法来修复它的方式我想要的 我想执行一个包含的linux命令来选择一批文件 来自php 我正在使用php生成一个“随机”数量的文件,然后希望执行一个shell脚本,该脚本将对这些文件进行处理 以下是我的bash版本: “$echo$BASH\u版本” 4.1.5(1)-发布 举个简单的例子,假设我创建了以下文件: touch/tmp/file_{1..12}.xml使用php exec启动带有大括号扩展的linux命令,php,linux,bash,shell,Php,Linux,Bash,Shell,如何强制php exec()解释linux大括号扩展 我遇到了一个奇怪的行为,没有找到一个方法来修复它的方式我想要的 我想执行一个包含的linux命令来选择一批文件 来自php 我正在使用php生成一个“随机”数量的文件,然后希望执行一个shell脚本,该脚本将对这些文件进行处理 以下是我的bash版本: “$echo$BASH\u版本” 4.1.5(1)-发布 举个简单的例子,假设我创建了以下文件: touch/tmp/file_{1..12}.xml shell.sh #!/bin/sh
shell.sh
#!/bin/sh
FILES=$*
echo "\n\nFILES: $FILES"
for f in $FILES; do
echo Posting file $f
done
test.php
<?php
$cmd = "./shell.sh /tmp/file_{1..12}.xml";
echo"\n\nCOMMAND:\n".$cmd."\n\n";
var_dump(shell_exec($cmd));
我希望具有与从linux终端运行“/shell.sh/tmp/file_{1..12}.xml”相同的功能: $./shell.sh/tmp/file_{1..12}.xml 文件:/tmp/file_1.xml/tmp/file_2.xml/tmp/file_3.xml/tmp/file_4.xml/tmp/file_5.xml/tmp/file_6.xml/tmp/file_7.xml/tmp/file_8.xml/tmp/file_9.xml/tmp/file_10.xml/tmp/file_11.xml/tmp/file_12.xml 发布文件/tmp/file_1.xml 发布文件/tmp/file_2.xml 发布文件/tmp/file_3.xml 发布文件/tmp/file_4.xml 发布文件/tmp/file_5.xml 发布文件/tmp/file_6.xml 发布文件/tmp/file_7.xml 发布文件/tmp/file_8.xml 发布文件/tmp/file_9.xml 发布文件/tmp/file_10.xml 发布文件/tmp/file_11.xml 发布文件/tmp/file_12.xml 但我也尝试了带或不带escapeshellcmd() 使用exec($cmd)和其他函数,如system()或eval()。。。 他们中没有一个人做过这项工作 我知道我可以在php中执行foreach循环,但我确信有一种方法可以将此命令解释为从命令行启动。
- 正如所述,您使用
隐式调用shell脚本的“外部”shell在您的情况下可能不是Bash。这样,大括号扩展就永远不会发生,因为在调用程序之前没有shell能够扩展表达式(就像在交互式Bash会话中一样) 你可以shell\u exec()
- 尝试更改PHP的
调用的shell,但是shell\u exec()
- 使用程序和大括号表达式而不是大括号表达式调用
:/bin/bash
$cmd=“/bin/bash-c./shell.sh/tmp/file_{1..12}.xml'代码>
- 对脚本中的参数使用
,以展开大括号表达式eval
- 尝试更改PHP的
- 从
手册页: 如果用sh名称调用bash,它会尽可能地模仿sh的历史版本的启动行为,同时也符合POSIX标准。[…]当作为sh调用时,bash在读取启动文件后进入posix模式 试着改变bash(1)
到 如果您期望Bash行为(POSIX中没有大括号扩展)#!/bin/sh
- 如果上述所有操作都没有帮助,那么您应该确保通过执行
(在从PHP脚本调用程序时)激活大括号扩展。如果已关闭,则可以使用以下命令将其打开:set-o
set -o braceexpand
/bin/sh
)是否设置为非bash shell
$ php test.php
COMMAND:
./shell.sh /tmp/file_{1..12}.xml
string(555) "\n\nFILES: /tmp/file_1.xml /tmp/file_2.xml /tmp/file_3.xml /tmp/file_4.xml /tmp/file_5.xml /tmp/file_6.xml /tmp/file_7.xml /tmp/file_8.xml /tmp/file_9.xml /tmp/file_10.xml /tmp/file_11.xml /tmp/file_12.xml\nPosting file /tmp/file_1.xml\nPosting file /tmp/file_2.xml\nPosting file /tmp/file_3.xml\nPosting file /tmp/file_4.xml\nPosting file /tmp/file_5.xml\nPosting file /tmp/file_6.xml\nPosting file /tmp/file_7.xml\nPosting file /tmp/file_8.xml\nPosting file /tmp/file_9.xml\nPosting file /tmp/file_10.xml\nPost"...
你能试着像
shell_exec(“sh-c./shell.sh/tmp/file{1..12}.xml')那样做吗?
!添加/bin/bash-c'$cmd'对我来说很有用。(将#!/bin/sh改为#!/bin/bash not)---:@Bast很高兴听到这个消息。是的,如果您的程序在Bash中运行与否实际上与此无关,因为大括号扩展发生在调用它的shell中,而不是在执行它的shell中。除非您为步骤1选择了eval
方法。这也需要执行步骤2。
set -o braceexpand
$ php test.php
COMMAND:
./shell.sh /tmp/file_{1..12}.xml
string(555) "\n\nFILES: /tmp/file_1.xml /tmp/file_2.xml /tmp/file_3.xml /tmp/file_4.xml /tmp/file_5.xml /tmp/file_6.xml /tmp/file_7.xml /tmp/file_8.xml /tmp/file_9.xml /tmp/file_10.xml /tmp/file_11.xml /tmp/file_12.xml\nPosting file /tmp/file_1.xml\nPosting file /tmp/file_2.xml\nPosting file /tmp/file_3.xml\nPosting file /tmp/file_4.xml\nPosting file /tmp/file_5.xml\nPosting file /tmp/file_6.xml\nPosting file /tmp/file_7.xml\nPosting file /tmp/file_8.xml\nPosting file /tmp/file_9.xml\nPosting file /tmp/file_10.xml\nPost"...