Home arrow PHP arrow Page 2 - An Alternative to Perl: Shell Scripting With PHP

An Alternative to Perl: Shell Scripting With PHP

Who says you have to use Perl to shell script? Jayesh definetly doesn't, and in this article he will show you how to shell script with PHP instead!

Author Info:
By: Jayesh Jain
Rating: 4 stars4 stars4 stars4 stars4 stars / 34
October 29, 2002
  1. · An Alternative to Perl: Shell Scripting With PHP
  2. · What are PHP Shell Scripts?
  3. · Using Arguments in Scripts
  4. · Conclusion

print this article

An Alternative to Perl: Shell Scripting With PHP - What are PHP Shell Scripts?
(Page 2 of 4 )

Normally shells are interactive, but not always. This means that the shell accepts commands from you through your keyboard and executes them. But instead of issuing command one by one, we can store this sequence of commands in a text file and tell PHP to execute this text file instead of entering the commands manually. This is known as PHP shell scripting.

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:
  1. Shell script can take input from a user or file and output them on screen
  2. Useful to create your own commands/applications
  3. Don't have to reinvent the wheel
  4. 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 <path> (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

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.

There are three streams available in the PHP CLI, which are:
  1. stdin ('php://stdin')
  2. stdout ('php://stdout')
  3. 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");

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;

This following example shows you how to output text to an error stream:

$stderr = fopen('php://stderr', 'w');
fwrite($stderr,"There was an Error");

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.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials