If you are already familiar with PHP code for Web sites, then you'll find it works great for command-line scripting on Linux systems.

Gone are the days when Perl was the first-choice interpreted language for command-line scripting on Linux systems. These days, a number of choices are available, including Python, Ruby, and PHP. If you already write PHP code for Web sites and are familiar with the language, using PHP on the command-line works fantastic and fast.

Perhaps one of the biggest functions of any scripting language in a script is to manipulate files and obtain user input. PHP handles this with as much grace as any other scripting language.

For instance, to have PHP handle reading user-supplied input during script execution, use:

#!/usr/bin/php
<?php
function read_input()
{
$fp = fopen("/dev/stdin", "r");
$input = trim(fgets($fp, 255));
fclose($fp);
return $input;
}

printf("Please supply your name: ");
$name = read_input();
printf("\nHello, $name.\n");
?>
</code>

The read_input() function defined above will take input from STDIN, store it in the $input variable, trim all leading and trailing white space, and return it.

The same principle can be used to read and manipulate standard files; remember that to Linux STDIN is just another file (hence opening /dev/stdin in the above example).

#!/usr/bin/php
<?php

if (file_exists($argv[1]))
{
$file = $argv[1];
} else {
printf("ERROR: File '$file' does not exist!\n");
exit 1;
}
$data = file($file);
$c = 1;
foreach ($data as $line)
{
printf(sprintf("[%s]: %s", $c, $line));
$c++;
}

In the above example, the PHP script will read every line from the file passed on the command line and output it with the current line number preceding it. If the file does not exist, the script will print an error and exit with a return code of 1 (signifying an error; on a normal operation, the script would exit with a return code of 0). The file() function is used here as it reads every line of the file into an array (in this case, $data), which is then used in the foreach() statement to loop through the array, once per line in the file.

PHP is no longer strictly for Web-based programming. It can be used quite easily for command-line scripting and is both flexible and fast. Likewise, nearly everything you can do with a Web script, such as database manipulation, can be done with a command-line script in PHP just as easily.

Open Sourcery This was published in Open Sourcery, check every Monday for more stories

Related links

Comments

1

Mike - 11/11/06

It's almost like you don't even know what the words "fast", "easy", "works", "fantastic", and "grace" actually mean!!!

This post is ridiculous! PHP is none of those things. It's slow, broken, inconsistent and clunky!

PHP is one of the my top three languages in terms of lines of code written for employers. I have work experience with at least half a dozen. I have studied language design and implementation. I've reviewed PHP's source along side other dynamic languages. There is no language, (which i have experience with) which i have a lower opinion of!

I know this is a flame but I just don't care. PHP's lack of consistency and awkward implementation has wasted my time on occasions too numerous to count. If i can stop just one programmer or project manager from thinking that PHP will bring them one single advantage over most alternatives then this flame was worth it!!!

» Report offensive content

2

Mike - 11/11/06

It's almost like you don't even know what the words "fast", "easy", "works", "fantastic", and "grace" actually mean!!!

This post is ridiculous! PHP is none of those things. It's slow, broken, inconsistent and clunky!

PHP is one of the my top three languages in terms of lines of code written for employers. I have work experience with at least half a dozen. I have studied language design and implementation. I've reviewed PHP's source along side other dynamic languages. There is no language, (which i have experience with) which i have a lower opinion of!

I know this is a flame but I just don't care. PHP's lack of consistency and awkward implementation has wasted my time on occasions too numerous to count. If i can stop just one programmer or project manager from thinking that PHP will bring them one single advantage over most alternatives then this flame was worth it!!!

» Report offensive content

3

Sangamnath - 20/02/07

I have the ascii text file.I want to load those files to tables.Now am able to load but not all the records.The total records are 2,00,000.So can anyone help me to overcome this problem.


Thanks in advance

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

3

Sangamnath - 20/02/07

I have the ascii text file.I want to load those files to tables.Now am able to load but not all ... more

2

Mike - 11/11/06

It's almost like you don't even know what the words "fast", "easy", "works", "fantastic", and "grace" actually mean!!! This post is ridiculous! ... more

1

Mike - 11/11/06

It's almost like you don't even know what the words "fast", "easy", "works", "fantastic", and "grace" actually mean!!! This post is ridiculous! ... more

Log in


Sign up | Forgot your password?

What's on?