How to use PHP as a scripting language Shell

2010-12-03  来源:本站原创  分类:OS  人气:126 

Many people may have thought about using PHP to write some procedures like regular letters, but no way to time the implementation of PHP;

We all know, PHP is a very good dynamic web development languages (speed fast, short development cycle ... ....) But very few people are aware that PHP can also be a good language for writing Shell scripts, Shell scripts when PHP as the language of writing, he is not as powerful as Perl or Bash, but he has a good advantage, especially For me this is not very familiar with PHP but who are familiar with Perl.
Shell to use PHP as a scripting language, you have to compile PHP as a CGI binary, not the Apache model; compiled into the PHP binary CGI mode has some security problems, the solution can be found on the PHP Manual (http:/ / www.php.net).
In the beginning you may feel uncomfortable for the preparation of Shell scripts, but will gradually get better: the PHP dynamic web authoring as a general language and Shell scripting language as the only difference is that a Shell script needs to explain life in the first line The script path of the program:
#! / Usr / local / bin / php-q
PHP executable file after we added the parameter "-1", like this PHP will not output HTTPHeader (If you still need as dynamic Web pages, then you need to use the header function of your output HTTPHeader). Of course, in Shell script which you still need to use the PHP start and end tags:
<? Php code?>
Now let's look at an example in order to facilitate a better understanding of PHP, Shell scripting language as the use of:
#! / Usr / local / bin / php-q
<? Php
print ("Hello, world! \ n");
?>
The above program will output a simple "Hello, world!" To the monitor.

I. Shell script to pass parameters to the PHP:
As a Shell script, often adding in some of the parameters when running the program, PHP script, as Shell has a built-in array "$ argv", use "$ argv" array can easily Shell script to read when the parameters ( "$ argv [1]" corresponds to the first argument, "$ argv [2]" corresponds to the second argument, and so on). For example the following program:
#! / Usr / local / bin / php-q
<? Php
$ First_name = $ argv [1];
$ Last_name = $ argv [2];
printf ("Hello,% s% s! How are you today? \ n", $ first_name, $ last_name);
?>
The above code at run time requires two parameters, namely the first and last name, such as running like this:
[Dbrogdon @ artemis dbrogdon] $ scriptname.ph Darrell Brogdon
Shell script to display the above will output:
Hello, Darrell Brogdon! How are you today?
[Dbrogdon @ artemis dbrogdon] $
Dynamic web pages written in PHP as a language, they also contain "$ argv" in this array, however, and there are some differences: When the PHP scripting language as Shell when "$ argv [0]" corresponds to the script file name, and when When used in the preparation of dynamic web pages, "$ argv [1]" corresponds to the first argument is the QueryString.

Second, the preparation of an interactive Shell script:
If a Shell script to run only their own, lost the interaction, then have no meaning. When writing PHP scripts for Shell when, how to read user input information? Unfortunately PHP does not read user input their own function or method, but we can follow the example of other language to write a function to read user input "read":
<? Php
function read () {
$ Fp = fopen ('/ dev / stdin', 'r');
$ Input = fgets ($ fp, 255);
fclose ($ fp);
return $ input;
}
?>
Note that the above function is only for Unix systems (other systems need to make the appropriate changes). The above function will open a file pointer, and then read a line of not more than 255 bytes (that is, the role of fgets), then close the file pointer, the return to read information.
Now we can use the function "read" a program written in our previous 1 changed a bit, make him more with the "interactive": the
#! / Usr / local / bin / php-q
<? Php
function read () {
$ Fp = fopen ('/ dev / stdin', 'r');
$ Input = fgets ($ fp, 255);
fclose ($ fp);
return $ input;
}
print ("What is your first name?");
$ First_name = read ();
print ("What is your last name?");
$ Last_name = read ();
print ("\ nHello, $ first_name $ last_name! Nice to meet you! \ n");
?>
Preservation of the above program, run about, you may see an unexpected thing: the last line of input into the three lines! This is because the "read" function to return information also includes the end of each line user newline "\ n", reserved to the first and last name in the end to remove the line breaks, the need to "read" function to modify it:
<? Php
function read () {
$ Fp = fopen ('/ dev / stdin', 'r');
$ Input = fgets ($ fp, 255);
fclose ($ fp);
$ Input = chop ($ input); / / Remove trailing whitespace
return $ input;
}
?>

Third, in other languages include PHP Shell script written in Shell script:
Sometimes we may need in other languages include Shell script Shell script written in PHP. Is very simple, here is a simple example:
#! / Bin / bash
echo This is the Bash section of the code.

/ Usr / local / bin / php-q <<EOF
<? Php
print ("This is the PHP section of the code \ n");
?>
EOF
In fact, is to call PHP to parse the following code, and then output; so, then try the following code:
#! / Bin / bash
echo This is the Bash section of the code.

/ Usr / local / bin / php-q <<EOF
<? Php
$ MyVar = 'PHP';
print ("This is the $ myVar section of the code \ n");
?>
EOF
Two of the code can be seen only difference is the second uses a variable "$ myVar", try running, PHP actually gives error messages: "Parse error: parse error in - on line 2"! This is because the Bash variable is "$ myVar", and the Bash parser to replace the first variable, in order to solve this problem, you need to prefix each variable PHP "\" escape character, then just the code changes as follows:
#! / Bin / bash
echo This is the Bash section of the code.

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

Well, now you can write your own PHP, Shell script, and I hope you all the best.

相关文章
  • How to use PHP as a scripting language Shell 2010-12-03

    Many people may have thought about using PHP to write some procedures like regular letters, but no way to time the implementation of PHP; We all know, PHP is a very good dynamic web development languages (speed fast, short development cycle ... ....)

  • JDK6 new features: Scripting language support (Scripting) 2010-03-29

    Overview JDK6 increased its support for scripting languages (JSR 223), theory is the scripting language to compile into bytecode, so scripting languages can also enjoy the Java platform, many advantages, including portability, safety, etc. In additio

  • What is a compiled language. Explanatory language and scripting language 2010-03-12

    The computer can not directly understand the high-level language, can only understand machine language directly, it must be high-level language should be translated into machine language, the computer can be value-based high-level language program. T

  • LUA scripting language entry 2010-03-28

    LUA scripting language entry Preliminary Study Lua Programming: Mu Feng (Second Life members) This article comes from CSDN blog: http://blog.csdn.net/skyremember/archive/2008/10/17/3089447.aspx In this article, I want to tell you how to Lua programmi

  • JScript.VBScript use the message box. Dialog - scripting language 2010-08-28

    The use of JScript message box ------------------------------------- Use warnings, tips and warnings can be used to confirm, confirm and prompt a message box to get user input. The message box is a window object's interface methods. As the window obj

  • Python scripting language is very useful 2010-12-25

    Python scripting language ---------------- very useful Today, do not know which nerve short-circuit, even started to pay attention Python. Internet casually looked down a document, think this thing is too practical it! Might be possible to carefully

  • C-based scripting language development technology-csp 2011-05-01

    What is eybuild http://www.eybuild.com/develop/index.htm eyBuild, is short for Easy Build, which is the CSP (C Language Service Page) development kit in general. eyBuild WEB applications on embedded devices is the ideal development tool, with a WEB s

  • What scripting language is 2010-08-30

    Script (script) is to use a particular description language, based on the format of a certain executable file, also called macro or batch file. Script usually called by the application and implementation of the interim. Various types of scripts is no

  • Found a J2me scripting language 2010-09-08

    Previously been concerned about the kahlua, accidentally discovered today there is a small language Hecl, first make a mark, you can learn the next script compile, interpret, and how to embed into other languages.

  • java scripting language called notebook (jython, jruby, groovy) 2010-11-14

    There are two ways 1.java se 6 after the jsr 223 specification for implementation ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngineManager scriptEngine = factory.getEngineByName("javascript");// Or "js" scriptEngine

  • Linux, compile and install Lua scripting language environment 2011-05-01

    Lua is undoubtedly an air of the East - "simple but not simple," I like it. My development machine is: RedHat Linux AS 5 First, Lua's official website (http://www.lua.org/) Download the latest release package (PS: I downloaded the lua-5.1.4.tar.

  • Web Security study notes and 5 - into the Web scripting language 2010-03-05

    Dynamic Execution Vulnerability PHP's eval ASP-execute Wscript.Shell File that contains the vulnerability There PHP remote file include vulnerability http://wahh-app.com/main.php?Country=US Applications will be handled as follows $ Counrty = $ _GET [

  • VBA Excel macro scripting language processing test file copy 2010-11-29

    ' The specified file directory replication tools ( Can a user based on the svn version-controlled project to do incremental publishing purpose use ) ' Author : Xu fruit ' Date :2010-11-26 Sub copyfiles() On Error GoTo errorflag ' Specifies the line n

  • Pyronaridine listing! Ruby programming language 2009-03-01

    Interactive Net Purchase Excellence Amazon purchase [Original title] The Ruby Programming Language Publishing ] [ O'Reilly Media, Inc. [Author] (United States) David Flanagan; Yukihiro Matsumoto Translator [] LIAO Zhi-gang; Zhang Wo [Name] Books O'Re

  • The Ruby Programming Language Introduction 2009-05-19

    Click here to download chm part1 Click here to download pdf Click here to download chm part2 Tip: chm version 【Description】 Details of the book Ruby 1.8 and 1.9 versions of the various aspects. Ruby in a brief summary, the book details the following:

  • jdk1.6 on scripting support 2010-03-31

    JDK1.6 Added java script support of dynamic languages for a variety of procedures, provides a unified way, you can run the java code when the call script. Java SE6 is implemented in JSR223. This is a script framework that provides for the internal sc

  • Is the nature of language and software data structures and algorithms 2010-02-25

    1. Language is a data structure and algorithm is composed of software, too. 2. The language of data structures and algorithms are those defined by the language definition (coded), and software, in addition to the language's own data structures and al

  • Faramita draft language design 0.1 - truly mobile code (mobile code) 2010-04-19

    Faramita draft language design 0.1 Advertising language (not really): The real mobile code (mobile code), a real distributed computing Abbreviation explanation: RPC: Remote Procedure Call Remote Call DSL: Domain Specific Language Domain Specific Lang

  • [Change] javascript: the world's most misunderstood programming language 2010-04-24

    JavaScript, is the world's most popular programming languages. In fact the world's personal computers are installed in each and in active use at least one JavaScript interpreter. JavaScript's popularity is due entirely to his WWW scripting language o

  • Scala will be the launching of the modern language of Web 2.0 2010-05-09

    Scala will be the launching of the modern Web 2.0 Language ( Reprinted ) A Twitter developer said, Scala will be the launching of the modern Web2.0 language. LinkedIn also use this language. Similarly, many other large companies such as Sony Picture,