Home arrow PHP arrow Page 3 - An Alternative to Perl: Shell Scripting With PHP
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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

An Alternative to Perl: Shell Scripting With PHP - Using Arguments in Scripts
(Page 3 of 4 )

As you all know, we can use arguments with PHP (remember, world.php was an argument to the PHP interpreter). Similarly, we can use arguments in our scripts too. Curious? Let's take a look...

All of the arguments passed to your script are stored in a zero based global array variable called $argv. There is also another global variable, $argc, which holds number of arguments passed to the script (For all of those coming from a C/C++ background, this should be familiar to you).

One thing you need to remember is that $argc will always be one or more never zero. This is because the name of the script being interpreted is always the first argument to the PHP interpreter. Here's the code, which will display the total number of arguments passed. It will also will display what those arguments are arguments:

<?
echo "Total argument passed are : $argc \n";
for( $i = 0 ; $i <= $argc -1 ;$i++)
{
echo "Argument $i : $argv[$i] \n";
}
?>


Assuming this code is entered into a file called argument.php, you could test this script by running something like this:

php argument.php arg1 arg2

Here's the output:



Using PHP Script Instead of Perl Scripts
As you must have guessed so far, the PHP executable can run independently from the web server. If you want to run your PHP scripts instead of Perl scripts -- which should be transparent to the system you need to add a shebang to the top of the PHP script that you want to execute. A shebang looks like this:

#!/usr/bin/php

This will tell the UNIX system that it needs to run the PHP interpreter for the following script, and the good thing is that this line will be ignored when you run the script under a Windows environment, thus you can write PHP scripts that are not operating system specific:

#!/usr/bin/php

<?php

// your PHP code
// goes here

?>
 

Don't forget to add the PHP tags in the script file, otherwise PHP will not interpret it properly. If you want to suppress the PHP headers, use #!/usr/bin/php q. Similarly you can also use any other PHP arguments that we discussed earlier by specifying them after the shebang.

Configuring Windows to Executing PHP Scripts
If you plan to run PHP scripts from a command line on a Windows machine, then you need to associate the PHP files with the PHP interpreter. To do this, open Windows explorer, click on the tools menu and select folder options. Next, click on the file types tab and select the new button. Type .php into the file extension box and click OK:



You now need to select the PHP entry in the registered file types list box and click on the advanced button. Click new and type "Run" in the action box. In the "Application Used to Perform Actions" box, type C:\PHP\PHP.exe "%1" %* (change the PHP path if its different on your machine, %* is used to send any command line arguments). Click OK, and then OK again and then the close button:



With this step completed, Windows is now configured to run PHP Scripts. To try it out, just double click on any PHP file using Windows Explorer to see it running.

You can also register files with different PHP extensions, such as .php3 and .php4 using the same method mentioned above.

blog comments powered by Disqus
PHP ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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