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

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?