Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198495
  • 博文数量: 264
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 2740
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-03 13:25
文章分类

全部博文(264)

文章存档

2011年(1)

2009年(263)

我的朋友

分类:

2009-06-03 16:16:39

正如我们大多数人所知,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 标签:
复制代码
现在让我们从经典的 Hello World 例子开始深入研究吧:
  1. #!/usr/local/bin/php -q
  2. print("Hello World!\n");
  3. ?>

如大家所知,我们在屏幕上简单的输出:Hello World!


·向脚本传递参数

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

  4. print("Hello, $first_name $last_name! How are you today?\n");
  5. ?>

在上面的脚本中,我们传递两个将被显示的参数:
[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. function read() {
  2.     $fp=fopen("/dev/stdin", "r");//译注:些处推荐用php://stdin,下同。
  3.     $input=fgets($fp, 255);
  4.     fclose($fp);

  5.     return $input;
  6. }

  7. ?>

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

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

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

  6.     return $input;
  7. }

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

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

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

  13. ?>

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

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

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

  7. ?>

·将 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.     print("This is the PHP section of the code\n");
  5. ?>
  6. EOF

非常简单吧?当你添加一个变量的时候就要小心了,看下面这个代码片断:
  1. #!/bin/bash
  2. echo This is the Bash section of the code.

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

你将会得到一个错误:
Parse error: parse error in - on line 2
要修正这个错误,你需要转义代码中所有的 '$' 字符:
  1. #!/bin/bash
  2. echo This is the Bash section of the code.

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

现在你可以用 PHP 开始创建自己的 Shell 脚本了。


原文地址:
译者:姜运涛


阅读(121) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~