热门关键字:  ubuntu  分区  Fedora  linux系统进程  函数

当前位置 :| 主页>Linux教程>编程开发>PHP>

将PHP作为Shell脚本语言使用

来源: 作者: 时间:2007-08-28 Tag: 点击:

  三、在其他语言编写的Shell脚本中包含PHP编写的Shell脚本:

  有时候我们可能需要在其他语言编写的Shell脚本中包含PHP编写的Shell脚本。其实非常简单,下面是一个简单的例子:

  

#!/bin/bash 
  echo This is the Bash section of the code.

  /usr/local/bin/php -q << EOF
    print("This is the PHP section of the code\n");
  ?>
  EOF

  其实就是调用PHP来解析下面的代码,然后输出;那么,再试试下面的代码:

  

#!/bin/bash 
  echo This is the Bash section of the code.

  /usr/local/bin/php -q << EOF
    $myVar = 'PHP';
  print("This is the $myVar section of the code\n");
  ?>
  EOF

  可以看出两次的代码唯一的不同就是第二次使用了一个变量“$myVar”,试试运行,PHP竟然给出出错的信息:“Parse error: parse error in - on line 2”!这是因为Bash中的变量也是“$myVar”,而Bash解析器先将变量给替换掉了,要想解决这个问题,你需要在每个PHP的变量前面加上“\”转义符,那么刚才的代码修改如下:

  

#!/bin/bash 
  echo This is the Bash section of the code.

  /usr/local/bin/php -q << EOF
    \$myVar = 'PHP';
  print("This is the \$myVar section of the code\n");
  ?>
  EOF

  好了,现在你可以用PHP编写你自己的Shell脚本了,希望你一切顺利。


上一篇:没有了
下一篇:php上传文件大小限制大全
最新评论共有 4 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表