doulan1866 2011-11-09 14:44
浏览 51

将php变量传递给使用shflags的bash脚本

I am trying to make a PHP program triggered by a web submit tell a bash script to run with a single command line parameter. I am using the shflags command line parser for bash.

The pertinent part of the PHP script is as follows:

// generate unique filename
    $destinationFolder = Mage::getBaseDir('media') . DS . 'webforms' . DS . 'xml';
    $filename = $destinationFolder . DS . $result->getId().'.xml';


    // create folder
    if (!(@is_dir($destinationFolder) || @mkdir($destinationFolder, 0777, true))) {
        throw new Exception("Unable to create directory '{$destinationFolder}'.");
    }

    // export to file
    $xmlObject->getNode()->asNiceXml($filename);

    // define parameters to pass

    exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename);      

}

}
?>

The bash script (xform.sh) (just a test script) is as follows.

#!/bin/bash
. ./shflags

echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/$$".txt"
echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt

DEFINE_string 'xmlfilename' 'test' 'filename of current x.xml file from webforms' 'Z'


FLAGS "$@" || exit 1
eval set -- "${FLAGS_argv}"

echo "xml file was" ${FLAGS_xmlfilename} >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt

The bash script works correctly from the command line, i.e.

$xform.sh --xmlfilename 1.xml

writes "xml file was 1.xml" to the foo.txt file.

When the PHP script is triggered from the web, the first part works correctly, i.e. it writes "foo" to the two target files, foo.txt and $$.txt. However, the xmlfilename variable is not coming along, and I really need that file name to be passed to the command line! (Note I should not need to use escapeshellarg because the file name is generated by my PHP program, not by user input.)

I have checked all the file permissions I can think of. xform.sh and shflags are both members of the www-data (Apache) group, owned by Apache, and a+x.

My suspicions are that the problem is related either to a) my PHP exec syntax or b) file permissions. Everything works as intended except the bit after xform.sh in this line!

exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename); 

UPDATE:

I've narrowed the problem some more by isolating the problem with some test code. With:

$script="echo";
$xmlfilename="$filename";
$target=">> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt";

exec ("$script $xmlfilename $target");   

...

PHP correctly writes the $filename to foo.txt, so $script works when value is "echo" and $filename works too.

When I set $script to a different simple form of the xform script that (only) writes the data to the file, that also works correctly.

So the problem is specifically with something that happen when PHP tries to write the $filename as a command line variable. Does a script run by Apache need more permissions than usual if it includes a command line variable?

Sigh.

  • 写回答

1条回答 默认 最新

  • douyun3022 2011-11-09 14:52
    关注

    In your exec() call you have the flag as --xmlfile but you are calling it from the command line as --xmlfilename

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c