Useful GREP_OPTIONS while searching for text

export GREP_OPTIONS="--color=auto -I"

This adds color and excludes binary matches


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Adding kbd tag functionality to WordPress

The <kbd> tags on StackOverflow look awesome. They’re awesome for blogging about keyboard shortcuts. I’d been looking all over the place for these, while I actually stumbled upon Jimmy Peña’s implementation of the StackOverflow CSS for <kbd> tags. You can find his post here.

The custom css that makes this possible is this:

kbd {
padding:0.1em 0.6em;
border:1px solid #ccc;
font-size:11px;
font-family:Arial,Helvetica,sans-serif;
background-color:#f7f7f7;
color:#333;
-moz-box-shadow:0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset;
-webkit-box-shadow:0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset;
box-shadow:0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
display:inline-block;
margin:0 0.1em;
text-shadow:0 1px 0 #fff;
line-height:1.4;
white-space:nowrap;
}

Once added to your css/Wordpress theme, you can start using the kdb tags, which look like these in actual webpage:

Alt+F2

To use these in WordPress, you can do without modifying your theme, if you use a plugin named My Custom CSS.

You can simply paste the CSS code into a box in the plugin’s options page, and straightaway start using it.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Pipe grep to less with color

You need to use the grep –color=always option with less -R

Eg:

#grep -inr 'robots' * --color=always | less -R

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Server Connectivity: Unable to reach any Akismet servers. A network problem or firewall is blocking all connections from your web server to Akismet.com

How to solve the following problem:

“Server Connectivity: Unable to reach any Akismet servers. A network problem or firewall is blocking all connections from your web server to Akismet.com. Akismet cannot work correctly until this is fixed.Please contact your web host or firewall administrator and give them this information about Akismet and firewalls”

To solve this, you need to enable a setting in your php.ini file:

grep -in --color 'allow_url_fopen' /var/www/fcgi/joel.co.in/php5/php.ini
891:allow_url_fopen = off

As you can see, allow_url_fopen is by default set to Off.

You need to set it to On

allow_url_fopen = on

And then restart Apache. Now Akismet will start to work!


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.
Automatically update all WordPress plugins from Bash

Automatically update all WordPress plugins from Bash

I’ve updated the script radically. WordPress had changed their site structure so that the older script would fail. With the latest updates, it should still work.

Latest version:
Writeup
Download

I was searching for an idea on how to automatically update all my WordPress plugins from Linux command line. Google introduced me to a script by Ventz Petkov. It worked very well with minor modifications. This script could automatically download all plugins from WordPress.org according to the subdirectories in the WordPress plugin directory.

However a minor drawback of the script is that it would overwrite all plugins without checking whether our plugins are already up to date. I thought this was a waste of resources, since the obvious use of such a script is to get it to run as a cron job at regular intervals. There is no need to keep downloading the same versions of plugins that you already have, countless number of times. Even Petkov has conceded as much, stating that such an addition would be quite useful.

So there you are, my first perl script. I would like to record due credits to Petkov for his method of parsing the wordpress.org website, and his framework. All that I’ve done is to add code for comparing the plugin versions that are in your plugins directory, with those on the website to see if there are available updates. If there are, only then are the files downloaded.

The latest version of the script can always be downloaded from here (New link).

The script can take command line argument. Argument #1 is the path to plugin directory. Argument #2 is the plugin to update (meaning you can manually update one plugin at a time), and is a remnant of the original script. For my purpose, I wont be using Argument #2, since I intend the script to be run automatically, updating as when updates are found.

Screenshots:

Script command line options:

--help or -h: Display this help message
--source=/path/to/plugindirectory or -s/path/to/plugindirectory
	Multiple folders can be specified at a time.
	Eg: ./updater.pl --source=/var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins,/var/www/virtual/joel.co.in/drjoel.in/htdocs/wp-content/plugins
--version or -v: Display script version information
--plugin=PluginName or -pPluginName: Specify one or more plugins to process instead of all plugins under source directory. To specify more than one plugin,
you can either repeat this option, like:
	Eg: ./updater.pl --plugin=nextgen-gallery --plugin=genesis-beta-tester
	  Or,
you can alternately specify a comma seperated list of plugins, like:
	Eg: ./updater.pl --plugin=nextgen-gallery,genesis-beta-tester
--wp-path= or -wp: Specify wordpress root directory instead of plugin directory
	Eg: ./updater.pl --wp-path=/var/www/virtual/joel.co.in/droidzone.in/htdocs,/var/www/virtual/joel.co.in/vettathu.com/htdocs

Examples:

/root/wordpress_plugin_updater/updater.pl -w/var/www/virtual/joel.co.in/droidzone.in/htdocs

/root/wordpress_plugin_updater/beta.pl -s/var/www/virtual/joel.co.in/droidzone.in/htdocs/wp-content/plugins

If running from cron:

/usr/bin/perl /path/to/updater.pl -s/var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins

An example crontab entry would be:

* */6 * * * /usr/bin/perl /root/bash-advanced-scripts/updater.pl -s/var/www/virtual/joel.co.in/drjoel.in/htdocs/wp-content/plugins &gt;&gt; /root/wordpress_update.log

Here the script is being run sixth hourly every day, and a log appended. You can have multiple entries for multiple blogs.

The following line will automatically flush log files, keeping last 6 days of backups:

0 0 */3 * * mv /root/wordpress_update.log /root/wordpress_update.log.old

You can see the list of updated plugins by:

[[email protected]] ~/temp #cat /root/wordpress_update.log | grep -in 'updating' --color
83:Processing plugin: easy-contact-forms | Local version 1.4 | Remote version 1.4.2 | Updating now..
158:Processing plugin: wordpress-importer | Local version 0.6 | Remote version 0.6.1 | Updating now..
68527:Processing plugin: fancier-author-box | Local version 1.0.5 | Remote version 1.0.6.1 | Updating now..
68910:Processing plugin: fancier-author-box | Local version 1.0.5 | Remote version 1.0.6.1 | Updating now..
90869:Processing plugin: simple-backup | Local version 2.7.1 | Remote version 2.7.2 | Updating now..
91357:Processing plugin: look-see-security-scanner | Local version 13.01 | Remote version 13.04 | Updating now..
91740:Processing plugin: look-see-security-scanner | Local version 13.01 | Remote version 13.04 | Updating now..
113638:Processing plugin: bulletproof-security | Local version .48 | Remote version .48.1 | Updating now..
113686:Processing plugin: bulletproof-security | Local version .48 | Remote version .48.1 | Updating now..
113718:Processing plugin: bulletproof-security | Local version .48 | Remote version .48.1 | Updating now..

 

The current version as of 31/03/13 is included below:

#!/usr/bin/perl -w
# Original Author: Ventz Petkov
# Last updated by Original Author on
# Date: 06-15-2011
# Last: 12-20-2012
# Version: 2.0
#
# Rewritten by: Droidzone
# Date 31-03-2013 www.droidzone.in
# Version 3.0.0.2
# Usage:
# ./update-wp-plugins.pl
#   or
# ./update-wp-plugins.pl registered-name-of-plugin
# (and this works to update an exiting plugin or download+install a new one)

$progversion="3.0.0.2";
$debugmode=0;
use Term::ANSIColor;
print color 'bold blue';
print "Wordpress Plugin Updater script v$progversion.\n";
print color 'reset';
&amp;argparser(@$ARGV);

if(!defined($ARGV[0])) {
    dprint ("No arguments supplied for folder path");
	$path="/var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins";
	dprint ("Path was specified on the command line\n");
}
else {
	#my @values = split(',', $ARGV[0]);	
	#foreach my $val (@values) 
	$path=$ARGV[0];
	print "Working on directory: $path\n";	    
    if ( -d $path )
    {
		dprint ("Path verified\n");
    }
    else
    {
		dprint ("The path does not exist\n");
	exit;
    }
}

dprint ("The path was set as ".$path."\n");
print "Plugin directory:$path\n";
chdir($path);

use WWW::Mechanize;
#use File::Find;
my $mech = WWW::Mechanize-&gt;new();
$mech-&gt;agent_alias( 'Mac Safari' );
my $wp_base_url = "http://wordpress.org/extend/plugins";

################################################
# Add New plugins Here:
# Format:
#   'registered-name-of-plugin',
#
my (@plugins, @plugins_notfound, @filepath, @pluginversion);

use File::Find::Rule;
use File::Spec;

my @folders = File::Find::Rule-&gt;directory-&gt;maxdepth(1)
    -&gt;in( $path )
    ;

foreach my $i (@folders){
    ( $volume, $directories, $file ) = File::Spec-&gt;splitpath( $i );
	undef $volume, $directories;
    if ( $file ne "plugins" )
    {
        push @plugins, $file;
		push @filepath, $i;
		#print "Path: ".$i."\t Name: ".$file."\n";
    }
}

for (my $i = 0; $i &lt; @plugins ; $i++ ) 
{
    dprint ("Processing ".$plugins[$i]."...");
    #print "Filename:".$filepath[$i]."/".$plugins[$i].".php"."\n";
    $filename=$filepath[$i]."/".$plugins[$i].".php";
	$varfound=0;
    if ( -f $filename ) 
    {
		dprint ("Meta File found at default location.\n");				
		open("txt", $filename);
		while($line = &lt;txt&gt;) 
		{			
			if ( $line =~ /^\s*\**\s*\bVersion:(.*)/i )
			{
				$pluginversion[$i]=$1;
				dprint ("Version found in file ".$filename);						
				$varfound=1;										
				dprint ($pluginversion[$i]."\n");
				dprint ("Array Num ".$i." Stored plugin name:".$plugins[$i]." Version found and stored ".$pluginversion[$i]);
			}
		}
		close("txt");			
    }
    else
    {
		$searchpath=$path."/".$plugins[$i];
		@files = &lt;$searchpath/*.php&gt;;
		dprint ("Search path is ".$searchpath."\n");
OUT: 	foreach $file (@files) 
		{
			dprint ("Checking alternate php file: ".$file."\n");
			open("txt", $file);
			while($line = &lt;txt&gt;) 
			{
				#  $line =~ /^[\s\*]*Version:(.*)/i
				# Discussion on regex: http://stackoverflow.com/questions/15728671/perl-regex-logic-error
				if ( $line =~ /^\s*\**\s*\bVersion:(.*)/i )
				{
					$pluginversion[$i]=$1;
					dprint ("Version found in file ".$file);						
					$varfound=1;										
					dprint ($pluginversion[$i]."\n");
					dprint ("Array Num ".$i." Stored plugin name:".$plugins[$i]." Version found and stored ".$pluginversion[$i]);
					last OUT;
				}
			}
			close("txt");	
		}
    }
	push @plugins_notfound, $plugins[$i];
}

if ( ! $varfound) 
{
	print "\nCould not parse version no from the follwing plugins:\n";
	for (my $i = 0; $i &lt; @plugins_notfound ; $i++ ) 
	{
		print $i." ".$plugins_notfound[$i]."\n";
	}
}
else 
{
	dprint ("We found all version numbers");
}
print color 'red';
#print colored("Summary of scanning plugin directory", 'red'), "\n";
print "Summary of scanning plugin directory\n";
print "------------------------------------\n";
printf("%-4s %-45s %3s\n", "No", "Name", "Version");
print color 'reset';
#print "No:\tName\tVersion\n";
for (my $i = 0; $i &lt; @plugins ; $i++ ) 
{
	$v = $pluginversion[$i];
	$v =~ s/[^a-zA-Z0-9\.]*//g;	
	printf("%-4s %-45s %3s\n", $i, $plugins[$i], $v );
	#print "$i\t$plugins[$i]\t$pluginversion[$i]\n";
}
print "\n";

################################################
$pluginsdone=0;

if(defined($ARGV[1])) {
    my $name = $ARGV[1];
    &amp;update_plugin($name);
}
else {
	$i=-1;
    for my $name (@plugins) {
		$i++;
        &amp;update_plugin($name,$i);
    }
}

print "Plugin updation completed successfully.\n";

if ( $pluginsdone &gt; 1 )
{
	print "$pluginsdone plugin(s) were updated.\n";
}
else
{
	print "Plugins were already up-to-date. Nothing done.\n";
}

sub update_plugin {

    my $name = $_[0];
	my $index = $_[1];
    my $url = "$wp_base_url/$name";
    $mech-&gt;get( $url );
    my $page = $mech-&gt;content;
	$url="";
    my ($version,$description,$file) = "";
    if($page =~ /.*&lt;p class="button"&gt;&lt;a itemprop='downloadUrl' href='(.*)'&gt;Download Version (.*)&lt;\/a&gt;&lt;\/p&gt;.*/) 
	{
		$url = $1;
		$version = $2;
		if($page =~ /.*&lt;p itemprop="description" class="shortdesc"&gt;\n(\s+)?(.*  
	)(\s+)(\t+)?&lt;\/p&gt;.*/) 
		{
			$description = $2;
		}
		if($url =~ /http:\/\/downloads\.wordpress\.org\/plugin\/(.*)/) 
		{
			$file = $1;
		}
    }	
	$oldversion = $pluginversion[$index];
	$version =~ s/[^a-zA-Z0-9\.]*//g;	
	$oldversion =~ s/[^a-zA-Z0-9\.]*//g;

    print "Processing plugin: ";
	print colored($name, 'green');	
	print " | Local version ";
	print colored($oldversion, 'green');		
	print " | Remote version ";

	if ( $version eq $oldversion )
	{
		print colored($version, 'green');	
		print " | ";
		print colored("Already update\n", 'green');
	}
	else
	{
		print colored($version, 'red');	
		print " | ";
		$pluginsdone++;
		print colored("Updating now..\n\n", 'blue');
		#print "Updating plugin $name now";
		`/bin/rm -f $file`; print "Downloading: \t$url\n";
		`/usr/bin/wget -q $url`; print "Unzipping: \t$file\n";
		`/usr/bin/unzip -o $file`; 
		print colored("Installed: \t$name\n\n", 'green');		
		`/bin/rm -f $file`;
	}
}

sub read_extract
{
	my $pl_version="";
    open("txt", my $file=$_[0]);
    while($line = &lt;txt&gt;)
    {
		for ($line)
		{
		 s/^\s+//;
		 s/\s+$//;
		}			
        if ( $line =~ /^Version:|^version:|^\* Version:/ )
        {
            $pl_version=&amp;extract_version($line);
        }
    }
    close("txt");
	$pl_version;
}

sub extract_version
{
    my $line=$_[0];
    $string=substr($line,rindex($line, ":")+1);
    for ($string)
    {
     s/^\s+//;
     s/\s+$//;
    }
    $string;
}

sub dprint
{
	$debugtext=$_[0];
	if ($debugmode) 
	{
		print $debugtext."\n";
	}
}	

sub print_help 
{
	print "\nCommand syntax:";
	print "\n";
	print "wpupdater [path/to/plugin dir] [plugin name]\n";
	print "\nBoth arguments are optional\n\n";
}

sub argparser
{
	foreach (@ARGV) {
		$argu=$_;
		#print "Argument:$argu";
		if ($argu eq "--help")
		{
			&amp;print_help;
			exit;
		}
	}

}

You can add these as a cron job.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.
A better shell existence – Changing ls colors

A better shell existence – Changing ls colors

I wasnt satisfied with the default blue color shades for ls, and was exploring ways to change them. I happened to come across the dircolor resources.

To get the default ouput, do this:

dircolors -p &gt; ~/.dircolorsrc

Once this is done, you get a modifiable file with colors for everything. Edit it to your whim.

Then to make sure that the codes get loaded, add the following to your ~/.bashrc:

d=.dircolors
test -r $d &amp;&amp; eval "$(dircolors $d)"

More info can be found by:

info coreutils 'dircolors invocation'

coloredls

 

I finally decided on the following colors:

color1

I am attaching my .dircolor file.

Download “Roms” – Downloaded 0 times –

I chose

Dir-Orange Underline

Symlinks-Cyan

Pictures-Pink

Executables-Blue

Color chart:

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37-39=white
# 
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Setting default grep options

I find myself using grep with

grep -i -n –color ‘string’ filename

most of the time. As you know, -i is for Ignore case, and -n for Number line, and –color colorizes the output.

An extremely useful thing to have is to set the environment variable

export GREP_OPTIONS="--color -i -n"

So I just need to type:

grep 'string' filename

and the output is already colorized, and has line number and case ignored.

Read more.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Grep double pipe with less, preserving color

This one was a little troublesome to figure out

Basically I want to search for one word, get the 10 sentences following it, pipe into a grep for another word, and then show the result a screenful at a time

 

grep -R -i -n -A 10 -B 10 "$1" * --color=always | grep -R -i -n -A 2 -B 2 "$2" * --color=always | less -R

 

The color=always is unnecessary for the first pipe.

 

Alternately a slightly different approach is to search for both words (OR condition)

 

egrep -w 'profilelink|template name' * --color=always | less -R

 

where profilelink and “template name” are the two strings I want to search for.

The results differ. However I prefer the first approach because I want to search for the string template name, get the 10 sentences following it and see whether this context contains the next work I wish to search for.

 

Of course when you use the first approach, only the second string is color highlighted. You can pipe that into the second method and get color highlighting for both words. In effect this approach searches for files containing both strings, and highlights both words in the result.

This is what I’m talking about:

dblsearch ‘template name’ ‘profilelink’ | egrep -w ‘profilelink|template name’ –color=always | less -R

where dblsearch is the following bash script:

grep -R -i -n -A 10 -B 10 "$1" * --color=always | grep -R -i -n -A 2 -B 2 "$2" * --color=always | less -R

So the final bash script is this:

grep -R -i -n -A 12 -B 12 "$1" * --color=always | grep -R -i -n -A 12 -B 12 "$2" * --color=always | less -R | egrep -w -A 12 -B 12 "$stringA" --color=always | less -R

To run it:

dblsearch 'string 1' 'string 2'

 

Alternately, if you always want color on in your grep results, set the GREP_OPTIONS variable in .bashrc:

export GREP_OPTIONS='--color=always'

So finally, it becomes:

grep -in [searchterm] [filename] | less -R

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Working with Multiple Windows on Emacs

Split a window into two horizontally: [Ctrl-X] [3]
Split a window into two vertically: [Ctrl-X] [2]
Switch to [O]ther Window : [Ctrl-X] [O] Oh, not zero

Delete current window (remove split) : Ctrl-X 0 This is Ctrl+X and a Zero

My custom emacs file:
Options added are custom color, removed the split window on start.

(add-to-list 'load-path "/path/to/color-theme.el/file")
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-clarity)))
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:inherit nil :stipple nil :background "black" :foreground "white" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 140 :width normal :foundry "unknown" :family "Droid Sans Mono")))))
[/code]


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Bash, the Clipboard and Popup menus

Today I’ll be briefly touching upon ways to send text to clipboard from Bash, and to Open Popup windows.

Bash and the Clipboard

To copy to clipboard, you may use xclip (Or xsel).

The format is:

To send to primary clipboard (Middle click pastes this content)

echo "Something" | xclip[/code]

To send to secondary clipboard (Rightclick and Choose Paste to paste)

echo "Something else" | xclip -selection clipboard[/code]

Bash and Popups

Install zenity

sudo apt-get install zenity[/code]

Informational popups:

zenity --info --text 'Hello World!'[/code]

 

Dialog options

--calendar
Display calendar dialog

--entry
Display text entry dialog

--error
Display error dialog

--file-selection
Display file selection dialog

--info Display info dialog

--list Display list dialog

--notification
Display notification

--progress
Display progress indication dialog

--question
Display question dialog

--text-info
Display text information dialog

--warning
Display warning dialog

--scale
Display scale dialog

--color-selection
Display color selection dialog

General options

--title=TITLE
Set the dialog title

--window-icon=ICONPATH
Set the window icon with the path to an image. Alternatively, one of the four stock icons can be used: 'error', 'info', 'question' or 'warning'


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.