Tag Archives: shell

Running a php script with compatibility to php4/php5 installed on host machine

I had to write a php script to do some processing on xml input. I executed the code using this line:

php <script_name>.php

The code was working fine on Mac OSX operating system. So, I checked in the code and later I checked out the code on a linux machine using RHEL 4.6 operating system. When I gave the same command on my linux machine, it showed errors like unknown function. So, I checked the php versions installed on Mac OSX  machine and RHEL machine using the command below.

php -v

The output on Mac OSX was:

PHP 5.3.2 (cli) (built: Aug  7 2010 00:04:41)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

The output on RHEL machine was:

PHP 4.3.9 (cgi) (built: Sep 12 2007 11:09:31)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

So, the difference in the PHP version was the reason why the code written on Mac OSX which was actually a PHP5 script didn't run on the RHEL machine which had PHP4 installed. So, I had to do some modifications to the original PHP5 script to create a new PHP4 script which was now running as desired on RHEL machine.

I wanted to run the php script oblivious of the php version installed on the machine on which I am running it.  So, I had to write a shell script which extracted the php version installed on the machine and then select the php script which was compatible with the php version installed on this machine. The script I wrote is below.

# Variables to read php version
php4_version=`php -v | grep "PHP 4"`
php5_version=`php -v | grep "PHP 5"`

# One of the variables length should be exclusively greater than 0
if [ ${#php4_version} -gt 0 ]
then
 file="<php4_compatible_script>.php"
 elif [ ${#php5_version} -gt 0 ]
then
 file="<php5_compatible_script>.php"
else
  echo "Error: Unexpected php version"
fi
# Executing the php script
php $file

The script works by extracting the version from 'php -v' using 'grep' command. On a php4 version machine, php5_version variable would be empty and on a php5 version machine, php4_version variable would be empty. Using this notion, we check on the length of the variable to select the php script which would be compatible with the machine. Then in the last step we execute that php script.

Do let me know of better ways of achieving this result by leaving behind a comment.

Popping up a notification on completion of a shell command

It often happens that you execute a shell command which takes too long to execute and you waste your time checking the terminal from time to time to check if the command has been completed.

I was thinking it would be better if I can be notified by a pop-up message that the command has been executed.  Searching on the web, about creating a pop-up message using shell commands, i came across xmessage command. Then, i wrote a script in which i pass the command to be executed as a single argument by enclosing it in double quotes. By executing this script, my command starts executing and when it is completed, i get a pop-up message in center of screen notifying it's completion.

Here's the script:
# !/bin/sh
USAGE="USAGE: `basename $0`  shell_command"

if [ $# -eq 0 ]
then
echo $USAGE >&2
exit 1
fi

# parse command line arguments
if  [ $# -eq 1 ]
then
$1

xmessage -center command `echo $1` has been executed
fi

I named this script as reminder.sh. So, an example usage of this script to unzip a large file would be :

$ ./reminder.sh "tar xvzf filename.tar.gz"

Since bash completion doesn't work under quotes, it would be a better idea to type the actual command first using bash completion, then copy it and pass as an argument to reminder.sh. Maybe there exists better way of achieving the desired effect. If you know one, please leave behind a comment.