I love
php. Not only is it an extremely
powerful language for creating dynamic web pages, it also can be used as a command line
scripting language like
Perl or
BASH. There are several
ways to execute a script from the command line as well as multiple options on how
to handle particular variables. Since this page is merely a reminder for me on
how to do certain things I won't go into them here. You can find all you need
to know by checking out
this page.
Now let's see some tips, tricks, and tweaks!
To create a truly functional command line application you need to be able to interact
with the user. Allowing them to respond and answer questions opens up a whole
new world of wonderful things you can do with your scripts. Keep in mind that
there are also some pitfalls to allowing the user to interact with your application.
Errors, inaccurate data, malicious activity, the list goes on and on..
Here is a small script that accepts user input, provides a quick and dirty menu with
some error checking, and does a neat trick of modifying a line that has already
been printed to the screen. Check it out and I will briefly explain it at the bottom.
I should note here that this was written for the Unix/Linux environment so some things
will not work on other operating systems (the clear screen and line breaks just to
name a few).
#!/usr/bin/php
<?
system("clear");
echo "\n\nMy CLI Count Down Application!\n";
echo "----------------------------------------\n\n";
// *****************************************************
// Part #1 - Retrieving user input from the command line
// *****************************************************
$stdin = fopen("php://stdin", 'r');
echo "Please enter your first name: ";
$firstname = fgets($stdin, 100);
$firstname = trim($firstname);
fclose($stdin);
// *****************************************************
// *****************************************************
// Part #2 - A simple menu system
// *****************************************************
do {
system("clear");
echo "\n\nMy CLI Count Down Application!\n";
echo "----------------------------------------\n\n";
echo "What number do you want to count down from?\n";
echo "(1) 1,000\n";
echo "(2) 100\n";
echo "(3) 10\n";
echo "\n(0) Exit!\n";
echo "\nPlease enter a number between 0 and 3 : ";
$choice = trim(fgets(STDIN));
} while(!(($choice == "0") || ($choice == "1") || ($choice == "2") || ($choice == "3")));
// *****************************************************
system("clear");
switch($choice){
case "1":
$counter = 1000;
break;
case "2":
$counter = 100;
break;
case "3":
$counter = 10;
break;
case "0":
default:
echo "\n\nThanks for using my CLI count down application!\nExiting.....\n\n";
exit();
}
echo "\n\n".$firstname." wanted to count down from ".$counter." so here we go!\n";
echo " (press CTRL+C at anytime to exit this script)\n\n";
// *****************************************************
// Part #3 - Modifying a line of screen text
// *****************************************************
$counter2 = $counter;
while($counter2 >= 0){
$output_str = "Current count is ".$counter2." out of ".$counter.". ";
echo $output_str;
$line_size = strlen($output_str);
while($line_size >= 0){
echo "\010";
$line_size--;
}
$counter2--;
sleep(1);
}
// *****************************************************
echo "\n\nAll done!\n\n";
?>
Part #1
Accepts user input, first name, by opening a file pointer to STDIN and reading it. The script
will wait until either the ENTER key has been pressed or a maximum of 100 characters has
been reached (reference the fgets() function). You definitely would want to add a
significant amount error checking to restrict what can and can't be entered.
Part #2
This is a simple menu. It displays 4 options for the user to choose from and waits for their
response. If an option is chosen that is not valid, the script will not advance but instead
redisplays the menu until a correct choice has been made.
Part #3
This is the fun part. It took me awhile to figure out how to modify a line of text that
had already been printed to the screen. In this example, as the counter counts down the
current line is modified to show what the current location is in the set. This is done
by echoing a "Backspace" to the screen for each character in line that was
just printed. Two very important things to note here:
-
This will not work if a new line is printed at the end. As far as I know, there is
no way to "scroll" up to a previous line. If anyone knows how to do this
I would be very interested in knowing!
-
Notice that there is an additional space at the end of the line. Because a "Backspace"
does not necessarily delete a character but rather moves the cursor, the last character
in the line will remain. The extra space prevents this character from being added to
any replacement lines that are shorter then the previous line printed.