Geek stuff, Linux, Tutorials

Bash Script to Find and Replace in a Set of Files

Ok, so this might sound childish to the more experienced admins and programmers but I’ve always found the need to search and replace a string in multiple files. For example, if I have to work with an inexperienced programmer’s code, I might have to change the name of the database in a couple of dozen places. If you are in such a place, you might want to try the following script:

for f in submit_*; 
  do sed "s/old_db_name/new_db_name/" < $f > a_$f ; 
  mv a_$f $f ; 

The first line finds all files that match the pattern submit_*. The loop first calls sed, makes the replacement and outputs the file to a_$f. Finally, it renames the a_$f file to $f so that we get the original filename. So, there you go. You can make all sorts of complicated finds and replaces through regular expressions and unleash the power of sed on this script. Chao.

resources, Tutorials

Troubleshooting vTiger CRM Install on PHP 5.2

I recently had to deploy vTiger CRM 5 on a machine running PHP 5.2. Turned out that vTiger does not like PHP 5.2 because of some differences between PHP 5.0 and 5.2. As soon as you start the install script, you get the following error:

Cannot redeclare class DateTime

The problem is that vTiger defines its own DateTime class and so does PHP 5.2. The solution therefore (see vTiger wiki) was to rename the class in vTiger source. To do that, all you need to do is to run the following commands from the vtiger source root (e.g. /var/www/html/vtiger):

mv modules/Calendar/Date.php modules/Calendar/Date.php.back
sed s/ DateTime/ com_vtiger_DateTime/ < modules/Calendar/Date.php.back > modules/Calendar/Date.php
mv modules/Calendar/Appointment.php modules/Calendar/Appointment.php.back
sed s/ DateTime/ com_vtiger_DateTime/ < modules/Calendar/Appointment.php.back > modules/Calendar/Appointment.php
mv include/utils/RecurringType.php include/utils/RecurringType.php.back
sed s/ DateTime/ com_vtiger_DateTime/ < include/utils/RecurringType.php.back > include/utils/RecurringType.php

This script modifies the files Date.php, Appointment.php and RecurringType.php by first taking the backup of the file and then replacing all occurrences of ” DateTime” with ” com_vtiger_DateTime”. The backup is kept in the same location.

After that, you can run the install script and get to the login screen. When you login, you get a new error (something to this effect):

Catchable fatal error: Object of class <ClassName> could not be converted to string in /var/www/xxx on line yyy

The solution of this came from this great post. You need to create a custom error handler (called ErrorHandler.php) in the vtiger source root with the following content:

function compatibilityErrorHandler($errno, $errstr, $errfile, $errline)
  switch ($errno) {
    echo "Unknown error type: [$errno] $errstr n";

// set to the user defined error handler
$old_error_handler = set_error_handler("compatibilityErrorHandler", E_RECOVERABLE_ERROR);

Finally, you need to include this error handler through the For that, execute the following commands:

sed "/include('vtigerversion.php');/ a
require_once('ErrorHandler.php');" < >

Notice the line break after the a. The first command makes a backup. The second inserts the require_once directive after the line with the include directive.