UNIX Systems Administrator (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Using PHP As A Shell Scripting Language
As most of us already know, PHP is the best language for developing dynamic web pages available today. Not many people are aware that it can be used as a shell scripting language as well. While PHP as a shell script isn't as robust as Bash or Perl it does have definite advantages, especially if you're like me and are more proficient in PHP than you are in Perl.
The requirements for using PHP as a shell language is that you must compile PHP as a CGI binary instead of as an Apache module. There are certain security issues related to this so please refer to the PHP Manual when doing so.
Where the code is concerned, the only difference between a PHP shell script and a regular PHP web page is the existence of the standard shell call at the top of the script:
#!/usr/local/bin/php -q
We're using the '-q' switch so that the HTTP headers are suppressed. Also, you're still required to begin and end the script with the standard PHP tags:
<?php ?>
So let's delve into the standard code sample we all know and love:
#!/usr/local/bin/php -q
<?php

    
print("Hello, world!\n");

?>
This, as most of you know already (about a billion times over) will simply output to the screen "Hello, world!".
[ Next Page ]


Comments:
RE: how to configure phpMistoffeles07/03/07 16:48
How to get value from php to Shell Command ?Xiaohui Li05/21/07 05:36
RE: How to display ireport using shellscriptjayabalan02/09/06 06:02
RE: How can I get result from exec(); commandKrishma02/08/06 16:43
RE: sql loaderrambabu M11/22/05 01:40
how to configure phppraveen11/21/05 13:47
How to run java program using shell scriptRupali11/11/05 04:01
How to run java program using shell scriptRupali11/11/05 02:12
creating cgi-bin folder on windows machineviswnath11/01/05 14:56
RE: cron in cpanelTiger10/31/05 03:32
How to display ireport using shellscriptRohini10/28/05 03:40
RE: PHP and MySQL under command lineAnselmo Vara10/18/05 03:10
RE: php -q and php-4.0.6Prashant Kumar09/14/05 06:21
How to run a php script in DOS command promptAjay08/13/05 05:58
Capturing php result into shell scriptSantiago08/12/05 15:48
Printer Control Codesverynewtophp07/25/05 09:33
Printer Control Codesverynewtophp07/25/05 09:26
PHP Config ProblemSheetal Kaul04/25/05 13:41
if /dev/stdin doesen't workRuben04/18/05 05:06
Socket ProgrammingS.Vaishnavi04/05/05 09:38
RE: Printing PHP outputbelsam03/31/05 16:35
Passing parameters between php V'Raj03/11/05 03:26
RE: cron in cpanelGregory Neal03/05/05 14:57
RE: PHP and MySQL (and others) in command linkrislec02/26/05 18:12
RE: Printing PHP outputpychong02/25/05 12:07
How can I get output in WIN32 applicationfito02/21/05 12:03
get variable inside shell scriptrommy11/25/04 22:51
cron in cpanelAhmet UK01/10/04 02:41
Why not php-cli?Micheal09/11/03 11:10
RE: How can I get result from exec(); commandrafa06/17/03 17:34
RE: php -q and php-4.0.6Far Side05/29/03 16:21
Shell Scripting W/O PHP-CGIJerry Weida05/05/03 21:56
Solution: argv doesnt work with phpOmar Cromwell Mercado04/28/03 06:14
RE: Running other programs from PHPsrinivas03/06/03 02:00
Using the apache module and keeping it simplecolda11/20/02 03:33
RE: shell scripting with PHP as an APACHE modLindsay Haisley11/18/02 16:07
RE: shell scripting with PHP as an APACHE moddotwebbie10/26/02 20:58
RE: shell scripting with PHP as an APACHE moduleSimon10/24/02 04:18
Scan table records for Expired DateBill Lottman10/20/02 12:38
Please let me know..to connect bt php ,shellsteve09/30/02 04:33
csv file starts in row 2 ....harold myers09/29/02 01:25
RE: i can not find /usr/local/bin/phpSteven09/06/02 01:15
i can not find /usr/local/bin/phpSyber08/27/02 12:07
/dev/xx for Win32Masur08/27/02 10:43
help how do i set php up using cgiBen 08/24/02 20:15
PHP ScriptingShahram08/22/02 20:57
RE: PHP and MySQL (and others) in command linMatt L08/15/02 17:05
RE: PHP and MySQL under command linehung08/10/02 07:51
RE: help with running shell scripts using PHPSuhas08/06/02 05:03
help running shell scripts using PHP & Oraclekelete08/02/02 06:13
RE: help with running shell scripts using PHPvin08/01/02 02:35
PHP-EggUn-Thesis07/25/02 15:23
help with running shell scripts using PHPtanuja07/18/02 07:57
PHP + MySQL shell script causing core dumpMatt L07/11/02 09:46
RE: How I run php script from the command linSteve King07/01/02 14:46
RE: PHP and MySQL under command lineSteve King07/01/02 14:44
How I run php script from the command line?Enkhbold06/27/02 21:08
Exec programs with other userLeChuck06/25/02 11:13
RE: PHP and MySQL under command lineGary Ambrose06/09/02 21:57
Nice one..Shailesh Agrawal06/08/02 11:53
What execute php script in command lineTina05/24/02 05:07
Design methodologies Sandeep David05/22/02 06:39
popen doesn't execute programsJimmy Lin05/15/02 12:18
HTTP_SERVER_VARSJason05/13/02 17:35
RE: Importing Variables does not work :(Dravine05/06/02 10:20
RE: Win32 #!/usr/bin/php -q equivalentHeath05/03/02 10:41
Anybody recognize this language?Lucia Boone04/24/02 14:45
SessionKesor04/23/02 19:06
a multiline readjoshua04/21/02 03:19
Solution to run php binary on a shell.Vergoz Michael04/17/02 05:52
RE: Getting this to work under win32, MoreSteve Dillon04/12/02 10:27
RE: Getting this to work under win32, MoreJim04/08/02 13:35
How can I get result from exec(); command?AeY04/07/02 05:37
RE: Problem when adding more parameters to phpS Clark04/05/02 16:20
Problem when adding more parameters to phpAndres04/05/02 04:40
RE: sql loaderrob04/04/02 22:40
sql loaderdavid 04/01/02 17:50
PHP and MySQL under command lineCraig Lyons03/28/02 13:25
RE: Getting this to work under win32, MoreSteve Dillon03/27/02 10:30
Document support for shell scripts in windowschandra03/19/02 07:19
RE: shell scripting with PHP as an APACHE moduleAdann03/09/02 02:40
RE: command line program executionAmit03/09/02 01:07
RE: PHP shell scripting and php.iniIan Gibbons03/06/02 21:56
sh -cPaul03/01/02 20:49
PHP Shell for WindowsJim Rittenhouse02/10/02 13:48
RE: Win32 #!/usr/bin/php -q equivalentJean-Pierre Zuate02/08/02 08:58
Running other programs from PHPChristian Schildt02/06/02 10:28
Win32 #!/usr/bin/php -q equivalentBryan elliott02/01/02 00:45
the "Read" Function in Win32Bryan elliott02/01/02 00:41
My php4 is not compatible with apache_1.3.12Åntosh Mahadappa Dyade02/01/02 00:00
RE: shell scripting with PHP as an APACHE modmike d01/18/02 13:46
RE: scriptcah ilank01/18/02 03:51
how to create mail box from asp using php siri01/10/02 11:00
Printing PHP outputTom Skillman01/07/02 15:32
scriptAslam01/07/02 05:46
Mind over MatterNexus Knowledge12/22/01 14:12
RE: Getting this to work under win32, MoreOlivier M12/10/01 12:22
RE: Importing Variables does not work :)))Olivier M12/10/01 11:16
RE: Where is my php executable?Jim12/07/01 01:31
PHP shell and Apache_modJim12/07/01 00:59
RE: Where is my php executable?Tom Atwater12/01/01 12:35
RE: php -q and php-4.0.6Jonathan Puls11/27/01 19:40
RE: php -q and php-4.0.6nick11/27/01 18:15
PHP shell scripts when PHP is Apache ModulePaul Marshall11/27/01 16:41
php -q and php-4.0.6Jan Baggen11/27/01 08:06
RE: shell scripting with PHP as an APACHE modulesimeon11/16/01 13:40
Where is my php executable?Francesco Andreozzi11/14/01 06:33
pass variable to phpscripttedbundyjr11/10/01 07:39
RE: Importing Variables does not work :(tedbundyjr ezekiel11/10/01 07:16
RE: Importing Variables does not work :(obviously10/31/01 20:20
RE: subjectiveobviously10/31/01 20:15
php4.0.4Juliette10/25/01 04:51
Back/Forward Buttons - Yield Blank FormBrent10/02/01 11:42
subjectivesn0k0ne08/30/01 11:32
BatchWebert08/27/01 12:07
Getting this to work under win32Steve Dillon08/27/01 09:14
Segmentation FaultErik de Bruijn08/17/01 06:20
Importing Variables does not work :(s0enke08/13/01 09:40
Installing both Apache mod and commandlineFloyd07/31/01 11:57
Start with PHPAles Kyral07/27/01 04:23
Use "con" instead "/dev/stdin" for PHP in WinBetha Sidik07/19/01 08:07
RE: Removing HTML tags from errors and warningsAdrian Kubala07/18/01 11:11
shell script with phpsam07/17/01 02:45
Web InterfaceAndy07/16/01 14:47
Sending an http cookie from shell scriptGlen Johnson07/13/01 11:43
Where should i start program PHP codessam07/05/01 08:45
RE: Readin more then just the first \n from stdinRoderick A. Carder-Russell06/15/01 06:40
switchesThomas Ruschival06/15/01 04:14
php://stdin have a bugJames Lin05/10/01 01:34
RE: Removing HTML tags from errors and warningsGildas05/09/01 05:11
Readin more then just the first \n from stdinscoob05/09/01 02:46
RE: Removing HTML tags from errors and warningsRyan Hatch05/08/01 19:08
web-masterIrina Lihacheva04/30/01 21:02
RE: Varibles and Shell ScriptingMark Walker04/29/01 06:00
Varibles and Shell Scriptingdigdan04/26/01 19:51
is it possible to run shell script as root ussandeep k mundra04/22/01 08:58
RE: shell scripting with PHP as an APACHE moduleVSutra04/17/01 00:54
Removing HTML tags from errors and warningsDon04/14/01 00:42
command line program executionregina02/21/01 18:18
RE: shell scripting with PHP as an APACHE moduleregina02/21/01 18:16
RE: PHP shell scripting and php.iniMichael Mauch02/15/01 14:35
php://stdin instead of /dev/stdinMichael Mauch02/15/01 14:25
RE: bug reportMichael Mauch02/15/01 14:22
PHP shell scripting and php.iniTolli01/30/01 07:06
bug reportBert01/25/01 09:08
RE: shell scripting with PHP as an APACHE moduleIshtiaq01/07/01 10:05
Couple thingsYing Zhang11/28/00 15:54
RE: shell scripting with PHP as an APACHE moduleJames Oakley10/11/00 23:01
both?remi10/04/00 16:34
shell scripting with PHP as an APACHE moduleSrinivas R. Rao09/27/00 21:51
shell scripting with PHP as an APACHE moduleSrinivas R. Rao09/27/00 16:23
Nice job!Derek Meiresonne07/26/00 15:08
sh roshambophilip olson07/17/00 23:06
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.