用 PHP 作为 Shell 脚本语言

来源:互联网 发布:网络推广思路 编辑:程序博客网 时间:2024/06/06 05:12
[转载请注明作者、译者和出处]
Author: Darrell Brogdon
译: 姜运涛

原文地址:http://www.phpbuilder.com/columns/darrell20000319.php3?page=1

译注:由于本人水平有限,在翻译的过程中难免会有纰漏,如果有什么问题请发邮件与我联系:jiangyuntao#gmail.com。

正如我们大多数人所知,PHP 是当今开发动态网页的最佳语言。但很多人没有意识到用它来做 Shell 脚本也是非常不错的。PHP 作为 Shell 脚本的时候不会像 Bash 或是 Perl 那样在这方面是强项。但如果你像我一样懂 PHP 比 Perl 多一些,那么也将会有一些明显的优势。

使用 PHP 作为 Shell 语言的条件是你必须编译其为二进制的 CGI,而非 Apache 模块。这样做肯定会有一些安全问题,所以请先查阅 PHP 手册。

使用 PHP 作为 Shell 脚本与用其写动态网页在代码上的一点不同,就是你必须要像其他 Shell 一样,先在顶部写这样一段代码:
  1. #!/usr/local/bin/php -q
复制代码
我们使用 '-q' 是为了屏蔽 PHP 发送的 HTTP 头。当然,你仍需要在脚本的头部和底部使用标准的 PHP 标签:
  1. <?php ?>
复制代码
现在让我们从经典的 Hello World 例子开始深入研究吧:
  1. #!/usr/local/bin/php -q
  2. <?php
  3. print("Hello World!\n");
  4. ?>
复制代码
如大家所知,我们在屏幕上简单的输出:Hello World!


·向脚本传递参数

通常你可能需要向一个脚本传递参数,在 PHP 中用一个内建的 '$argv' 数组将会很容易实现,如下例如示:
  1. #!/usr/local/bin/php -q
  2. <?php
  3. $first_name = $argv[1];
  4. $last_name  = $argv[2];

  5. print("Hello, $first_name $last_name! How are you today?\n");
  6. ?>
复制代码
在上面的脚本中,我们传递两个将被显示的参数:

[yc@local yc]$scriptname.php Yeto Chiang


输出:

Hello, Yeto Chiang! How are you today?
[yc@local yc]$


在使用 PHP 作为 Shell 脚本与做动态网页之间 '$argv' 数组有一个主要的不同点。用作 Shell 脚本时,'$argv[0]' 就是你的脚本名。而做动态网页时,它就是你查询语句的第一个参数。


·让脚本有更强的交互性

我们怎样得到用户的输入呢?我们怎样创建一个真正交互的脚本呢?PHP 本身并没有一个可以读取 Shell 命令的函数,但是我们可以用下面的函数模拟一个:

*注:本函数仅适用于类 Unix 系统
  1. <?php

  2. function read() {
  3.     $fp=fopen("/dev/stdin", "r");//译注:些处推荐用php://stdin,下同。
  4.     $input=fgets($fp, 255);
  5.     fclose($fp);

  6.     return $input;
  7. }

  8. ?>
复制代码
这个函数打开一个标准输入文件的指针 (/dev/stdin on *nix),我们可以用该指针读取任意东西,直到大小达到 255 个字节、新行或是 EOF。这种情况下,最容易因为新行停止读取,然后它会关闭指针并返回数据。

现在我们修改前面的脚本,让其使用刚刚创建的 'read' 函数来等待用户输入:
  1. #!/usr/local/bin/php -q
  2. <?php

  3. function read() {
  4.     $fp=fopen("/dev/stdin", "r");
  5.     $input=fgets($fp, 255);
  6.     fclose($fp);

  7.     return $input;
  8. }

  9. print("What is your first name? ");
  10. $first_name = read();

  11. print("What is your last name? ");
  12. $last_name = read();

  13. print("\nHello, $first_name $last_name!  Nice to meet you!\n");

  14. ?>
复制代码
你可能发现了,当我们执行这个脚本的时候,本应显示成一行的最后一行被分割成三行。这是因为我的们的 'read' 函数同样也读取了新行。这一点很容易修正,只要在返回数据之前去掉新行就行了:
  1. <?php

  2. function read() {
  3.     $fp=fopen("/dev/stdin", "r");
  4.     $input=fgets($fp, 255);
  5.     fclose($fp);

  6.     return str_replace("\n", "", $input);
  7. }

  8. ?>
复制代码
·将 PHP Shell 脚本嵌入正规的 Shell 脚本

有的时候可能需要将 PHP Shell 嵌入到一段已经写好的 Bash 或是其他 Shell 中,这相当容易,但也要小心些。

首先是如何嵌入 PHP 代码:
  1. #!/bin/bash
  2. echo This is the Bash section of the code.

  3. /usr/local/bin/php -q << EOF
  4. <?php
  5.     print("This is the PHP section of the code\n");
  6. ?>
  7. EOF
复制代码
非常简单吧?当你添加一个变量的时候就要小心了,看下面这个代码片断:
  1. #!/bin/bash
  2. echo This is the Bash section of the code.

  3. /usr/local/bin/php -q << EOF
  4. <?php
  5.     $myVar = "PHP";
  6.     print("This is the $myVar section of the code.\n");
  7. ?>
  8. EOF
复制代码
你将会得到一个错误:

<b>Parse error</b>: parse error in <b>-</b> on line <b>2</b><br>


要修正这个错误,你需要转义代码中所有的 '$' 字符:
  1. #!/bin/bash
  2. echo This is the Bash section of the code.

  3. /usr/local/bin/php -q << EOF
  4. <?php
  5.         \$myVar = "PHP";
  6.         print("This is the \$myVar section of the code.\n");
  7. ?>
  8. EOF
复制代码
现在你可以用 PHP 开始创建自己的 Shell 脚本了。
0 0
原创粉丝点击