dongtun3259 2014-02-12 16:57
浏览 126
已采纳

将shell变量作为参数传递给PHP脚本

I want to pass shell variables to a PHP script. This is my attempt:

var1=`echo name_16`
var2=`echo /home/place/end/name_16`
php script.php ${var1} ${var2}

These variables contain special characters [)(*&^%$#@!~<>_] but no hyphens.

I am running PHP 4.3.9 cgi.

I attempt to pick up the variables in my PHP script thus:

    $fish = $argv[1];
    $sticks = $argv[2];

These variables come up as null. I've tried var_dump($argv); and this returns null as well.
Am I passing these parameters correctly or am I missing something here?

  • 写回答

2条回答 默认 最新

  • douhutongvm382381 2014-02-12 18:02
    关注

    I wanted to add this as a comment, but need the formatting ability of an answer to respond.

    There's something else wrong with the code. A basic test of arguments shows that the variables should work.

    Contents of foo.php:

    <?php
        print "$argv[1]
    $argv[2]
    " ;
    
        $x = $argv[1];
        $y = $argv[2];
    
        print "$x
    $y
    ";
    ?>
    

    Then the setup:

    $ var1=`echo name_16`
    $ var2=`echo /home/place/end/name_16`
    

    And execution:

    $ php foo.php ${var1} ${var2}
    name_16
    /home/place/end/name_16
    name_16
    /home/place/end/name_16
    $
    

    So the problem is not reproducible using the information provided. The above is run with PHP 5.2.6 (cli) (built: May 8 2008 08:53:44), so it is, perhaps, a difference in version behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?