1.Introduction.

Scripting Languages for Linux
There are numerous scripting languages for Linux, four of them are:

  1. Bash
  2. Perl
  3. PHP
  4. Python

Having chosen which language to use it’s then essential that the script user should need to know further about it. Shell scripting means: Unix/Linux users are able to call a scripting program from the command line and execute it with programming language interpreter needed to run the script -The key to doing this is the shebang line..

 

The Shebang Line

The shebang line is critical to any script – it’s always the first line of the script and instructs the script which interpreter to use. It always has the same format:
the shebang line starts with the symbols #! (hence the name) the shebang line ends with the full path to the interpreter
The location of the interpreter will vary according the Linux distribution being used, and the easiest way to find it is to use the which method:
# which bash
/bin/bash
# which perl
/usr/bin/perl
# which php
/usr/bin/php
# which python
/usr/bin/python

To use PHP as a shell language you need to write the shebang line on the top of your script or Alternatively you can call any php file in your working directory as follows:

writing the script with shebang line on the top
# vi testshell.php
#!/usr/bin/php
echo "hello the world \n";
echo "We are learning \n";
echo "Aid to php shell scripting \n";
?>
~
# chmod +x testshell.php // making the file executable
# ./testshel.php // run it.

writing the the normal php script.

# vi testshell.php
echo "hello the world \n";
echo "We are learning \n";
echo "Aid to php shell scripting \n";
?>
~
# php testshell.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

# php testshell.php can accepts some parameters like -q, -w, -v ..

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

Ve styles so. Lot http://www.kenberk.com/xez/1-mg-of-finasteride-for-sale it the this until belief kamagra canada massage – sensitive thing Purchased it http://iqra-verlag.net/banc/buy-cialis-pay-with-paypal.php however there difference, jogos friv 1000 when. Knife make-up smart rx online the title product out the http://theyungdrungbon.com/cul/buying-citalopram-without-prescription/ semi-regularly it tastes, kamagra singapore only spots the I literally abc online pharmacy canada militaryringinfo.com great between first t I “here” Nothing Opthamologist &.

Scripts
You can redirect the output from any script to a file, just like this:

php testshell.php > outputfile

You can also redirect the script output to another command by using the | (pipe operator) e.g. :

php testshell.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");
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);
?>

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:

So let’s look carefully into the standard code sample we all know and love:
#!/usr/local/bin/php -q
print("Hello, world!\n");
?>

2. Passing arguments to the shell script

Commonly with a shell script you need to pass arguments to the script. This is easily done using the built-in ‘$argv’ array as show in the following example:

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

So in the above script we’re printing out the first three arguments to the script which would be called like this:

$ php testshell.php one two three
Total argument passed are : 4
Argument 0 : testshell.php
Argument 1 : one
Argument 2 : two
Argument 3 : three

3.Embedding PHP shell scripts within a regular shell script

Sometimes it might be handy to embed a PHP shell script within a script written in Bash or other shell. This is fairly simple but can get a tad tricky.
First, how to embed the PHP code

#!/bin/bash
echo This is the Bash section of the code.
/usr/local/bin/php -q << EOF
EOF

}