≡ Menu

Shell scripting

This may come handy while writing cross-platform scripts.

If you don't want to commit to the idiosyncrasies of a specific shell running on a particular platform, try the Squirrel Shell. The Squirrel Shell provides an advanced, object-oriented scripting language that works equally well on UNIX, Linux, Mac OS X, and Windows systems. Write a script once, and run it anywhere.

Squirrel is a high level imperative/OO programming language, designed to be a light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games.

=> Speaking UNIX: The Squirrel portable shell and scripting language

Upgrade WordPress 2.7 in 3 Simple Steps

WordPress version 2.7 has been released and can be upgraded easily using UNIX / Linux shell prompt. A few assumptions:

  1. WordPress database name: wptheosblog
  2. WordPress directory name: /var/www/html/theos.in
  3. WordPress domain name: theos.in
  4. Operating system: Linux
WARNING! If you are not comfortable with UNIX shell prompt (IF YOU ARE new to a UNIX/Linux os), please follow traditional way of upgrading wordpress and DO NOT use the three Steps described here.

[click to continue…]

Linux / UNIX: Find Out If a Directory Exists or Not

I've already written a small tutorial about finding out if a file exists or not under Linux / UNIX bash shell. However, couple of our regular readers like to know more about a directory checking using if and test shell command.

General syntax to see if a directory exists or not

[ -d directory ]
test directory
See if a directory exists or not with NOT operator:
[ ! -d directory ]
! test directory

Find out if /tmp directory exists or not

Type the following command:
$ [ ! -d /tmp ] && echo 'Directory /tmp not found'
$ [ -d /tmp ] && echo 'Directory found' || echo 'Directory /tmp not found'

Sample Shell Script to gives message if directory exists

Here is a sample shell script:

if [ $# -ne 1 ]
	echo "Usage: $0 {dir-name}"
	exit 1
if [ -d "$DIR" ]
	echo "$DIR directory  exists!"
	echo "$DIR directory not found!"

This is an user contributed article.

Midnight Commander (mc) is an user-friendly text-based file manager UI for Unix. Using mc, you can browse the filesystem easily and manipulate the files and directories quickly. You will not miss the standard command line prompt, which is also available within the mc itself. If you are new to mc, Midnight Commander (mc) Guide: Powerful Text based File Manager for Unix article will give you a quick jumpstart. In this article, let us review how to solve couple of common annoyance about viewing a file in mc.

Use vi as default editor and viewer in mc

Mc uses mcedit for file editor and mcview for file viewer. Like most of you, I'm very comfortable with vi and would like to use vi for both viewing and editing than the mc's internal editor and viewer.

Launch mc in color mode by typing “mc -c” from the command line. Press F9 (or Esc followed by 9) to activate the top menu → select Options menu → select Configurations menu-item, which will display “Configure Options” dialog → de-select the check-box next to “Use Internal Edit” and “Use Internal View”, as shown below to disable, internal editor and viewer.

After this change, when you select a file and press Esc 3 to view or Esc 4 to edit, mc will use vi.

Fig.01 Mc configure options to disable internal editor and viewer

Fig.01 Mc configure options to disable internal editor and viewer

Change the Enter key behavior to view file using vi instead of executing it.

When you select a shell script and press enter, mc will execute it by default. Also, by default when you press enter on text files, nothing happens. I prefer to view the shell script when I press enter key. Also, I would like to view the text file using vi when I press enter key. You can achieve this by modifying the mc extension file as shown below.

Press F9 (or Esc followed by 9) to active the top menu. From command menu → select “Edit extension file” men-item → This will display the extension file. Go to the bottom of the extension file and change the value of open and view parameter values as shown below.

# Default target for anything not described above default/*

        Open=%var{EDITOR:vi} %f
        View=%var{EDITOR:vi} %f 

After the above change, when you press enter key on a shell script, it will open it in vi instead of executing it. This will also open text files in vi when you press enter after selecting it.

UNIX Korn Shell Scripting Tutorial / Guide

Korn shell (ksh), a command-line interface for Unix. The main advantage of ksh over the traditional Unix shell is in its use as a programming language. Since its conception, several features were gradually added, while maintaining strong backwards compatibility with the Bourne shell.

IBM has published Korn shell scripting - A beginner's guide:

Korn shell scripting can save you a lot of time and make your job so much easier. It can seem intimidating at first, but remember to always start out simple and build upon each and every script. Always follow the same steps: build your script header, define your variables, and error check your work. You just might find yourself trying to write a script for everything you do.

Korn shell scripting is something all UNIX users should learn how to use. Shell scripting provides you with the ability to automate many tasks and can save you a great deal of time. It may seem daunting at first, but with the right instruction you can become highly skilled in it. This article will teach you to write your own Korn shells scripts.

This is 3rd and the final installment for Urchin 6 web analytics software series. Once Urchin is installed, you need to configure tracking on your website. You need to install Urchin sensors - a small piece of javascript tracking
code on each of your website's pages. Usually all large site uses some sort of templating (themes) system.

Step # 1: Copy UTM files to webroot

You need to copy or softlink urchin.js and __utm.gif file to webroot from /usr/local/urchin/util/utm directory. If your webroot set at /home/lighttpd/cyberciti.biz/, enter:
# cp -v /usr/local/urchin/util/utm/* /home/lighttpd/cyberciti.biz/
Set appropriate file permissions:
# chown apache:apache /home/lighttpd/cyberciti.biz/urchin.js
# chown apache:apache /home/lighttpd/cyberciti.biz/__utm.gif

Step # 2: Add tracking HTML JS code in the HEAD section

On each page of your website, place the following tracking code right after the any META tags in the HEAD section:

<script src="/urchin.js" type="text/javascript">
<script type="text/javascript">

Also make sure each website or profiles set to Urchin Traffic Monitor (UTM) as the visitor tracking method.

Step # 3: Make sure Apache logs data with cookies

You need to enable cookies in your Apache logging, add following code your httpd.conf file:

LogFormat "%h %v %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" urchin

Find your domain <VirtualHost> entry for which you wish to enable this new logging format. Deactivate any existing TransferLog or CustomLog entries within a . Then insert the following new CustomLog entry, replacing the string path_to_log with the appropriate path to your log location:
CustomLog /var/log/httpd/cyberciti.biz/access.log urchin

A note about lighttpd web server

If you are using Lighttpd add following code to you lighttpd.conf file:

accesslog.format = "%h %v %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\""

Save and close web server configuration file. Restart the server:
# service httpd restart
# service lighttpd restart

How do I view reports?

Login to your account by visiting urchin admin url:
Click the Go To Report button to the right of each Profile Name to launch the reporting window for that Profile. The reporting window will allow you to view all available reports for the Profile i.e. website. Here is a sample report:

Fig.01: Urchin 6 Sample Report

Fig.01: Urchin 6 Sample Report (click to enlarge)


This series has shown you how to install and configure Google Urchin 6 under Red Hat Enterprise Linux 5.x. I suggest reading following section for more information.

Further readings:

Windows PowerShell vs UNIX BASH Shell

Shell scripting is fun. It is useful to create nice (perhaps ugly) things (read as solutions) in shell scripting. Now Windows got Powershell. But how does PowerShell measure up to traditional shells like Bash?

Linux Magazine's Marcus Nasarek compares Windows Vista PowerShell with Bash:

Both Bash and the Windows Vista PowerShell include commands for navigating directories, managing files, and launching other programs. System administration is an important duty for the shell, and Bash and PowerShell are equipped to help manage systems from the command prompt. Whereas Bash typically relies on a combination of newer tools and classic Unix utilities, the PowerShell has its own set of command-line programs. Windows refers to PowerShell commands as cmdlets. The PowerShell cmdlet called Get-Process is a counterpart to ps, and the cmdlet Get-Content corresponds to less. PowerShell differs significantly from previous Windows command shells.

Some time I need to work with on Windows Servers and I find this article interesting. However, I prefer to use Perl or Python for complicated stuff.