PHP shell scripts are just like batch files in MS-DOS, but they have more power
than the MS-DOS batch file, thanks to PHP.
Why Write Shell Scripts?
Here are some reasons why you may want to consider writing PHP shell scripts:
- Shell script can take input from a user or file and output them on screen
- Useful to create your own commands/applications
- Don't have to reinvent the wheel
- Can be used to automate some day to day tasks, such as backups
Getting
Started
Lets start with a small script to display the typical "Hello World" text. Create
a text file called world.php. Enter the following code into world.php and save
it in your PHP directory: <?
echo "Hello World";
?>
Open your command prompt change into the folder where PHP is installed and run
the following command:
php world.php
If you're surprised to see the output at the command prompt instead of in a web
browser, then welcome to the other dimension of PHP! You may have noticed that
the following header is also included in the output (PHP does this be default,
which also tells you the PHP version): X-Powered-By: PHP/4.2.3
Content-type: text/html
To suppress this HTTP header, we could run PHP with the following command line
parameter:
php -q world.php
Lets look at few of the command line options available with the PHP interpreter:
- -q (Quiet mode. Suppress HTTP Header output)
- -w (Display source with stripped comments and white space)
- -v (Version number)
- -c (With this option one can either specify a directory where to look
for php.ini or you can specify a custom INI file directly (which does not need
to be named php.ini)
- -d (This option allows you to set a custom value for any of the configuration
directives allowed in php.ini. The syntax is: -d configuration_directive[=value])
- -l (This option checks the syntax in the source file)
- -i (This command line option calls phpinfo() and prints out the results)
Using
Streams in Scripts
You can redirect the output from any script to a file, just like this: php world.php > outputfile
[Note] For all you Linux fans, you can also redirect the script
output to another command by using the | (pipe operator) e.g. : php world.php
| sort [End Note]
There are three streams available in the PHP CLI, which are:
- stdin ('php://stdin')
- stdout ('php://stdout')
- stderr ('php://stderr')
This following example will display "Hello World" in the output window using the
output stream: <?
$stdout = fopen('php://stdout', 'w');
fwrite($stdout,"Hello World");
fclose($stdout);
?>
This example will demonstrate how to use an input stream. It will accept input
from the user and wait until the user presses the enter key and then it will display
the text entered:
<?
$stdin = fopen('php://stdin', 'r');
echo "Please Enter your Name :";
$mystr = fgets($stdin,100);
echo "Your Name Is :n";
echo $mystr;
fclose($stdin);
?>
This following example shows you how to output text to an error
stream: <?
$stderr = fopen('php://stderr', 'w');
fwrite($stderr,"There was an Error");
fclose($stderr);
?>
OK, before we move ahead, there are a few things that you should know. The output
to the error stream is always sent to the error device (normally screen) and is
not sent to file or another command when redirecting the output. Always make sure
you close this stream once you are done with it. Please refer to the PHP manual
if you need more information on the fopen, fwrite, fgets and fclose functions.
Click
Here to Read the Full Article
About the Author:
Jayesh Jain is working as applications consultant for a health company in Auckland,
New Zealand. He has several years of n-Tier development experience and is currently
working with Visual Basic.NET to develop interactive client solutions. He has
a passion for Web development and in the spare time he likes to write articles.
Contact him at: jainjayesh74@yahoo.com
Read this newsletter at: http://www.sysadminnews.com/2003/0716.html |
|