BASH if statements: ‘too many arguments’ error

Posted by Christian Ashby on April 28, 2010

If you are writing a BASH script which searches for a file pattern in a folder using this syntax:

[-f {pattern}]

Instead, use the following syntax:

files=$(ls {pattern} 2> /dev/null | wc -l)
if [ "$files" != "0" ]

This can be replaced with a similar command using find if required.


Debian: apt-get interrupted, leaves files in bad state

Posted by Christian Ashby on April 19, 2010

For those using Debian or Ubuntu Linux, there are situations where the package management system can leave files open if a certain package fails, or if you cancel the installation using CTRL+C, you may see the following error when you try another package installation:

debconf: DbDriver "config": /var/cache/debconf/config.dat is locked by another process: Resource temporarily unavailable

Using the following command will tell you what’s using it:

fuser -v /var/cache/debconf/config.dat

This will give you the process ID in the right-hand column. You can then use kill {process ID} to remove this process and re run your apt-get command.


Oracle XE: Clearing out unwanted trace files

Posted by Christian Ashby on April 9, 2010

If left unchecked, Oracle XE installations can balloon in size quite quickly – this is due to the trace files being written by the server. The following run in a cron script can be used to remove files more than 7 days old.

find $ORACLE_HOME/../../../admin/$ORACLE_SID/bdump -name "*.trc" -mtime +7 -exec rm "{}" \;
find $ORACLE_HOME/../../../admin/$ORACLE_SID/udump -name "*.trc" -mtime +7 -exec rm "{}" \;
find $ORACLE_HOME/../../../admin/$ORACLE_SID/cdump -name "*.trc" -mtime +7 -exec rm "{}" \;

This tip was found and modified for XE in the following a useful article ‘Oracle Linux – Using the “find” command to manage files’.


Linux: Arrow keys don’t work?

Posted by Christian Ashby on March 26, 2010

Some Linux applications such as sqlplus still use readline to read what you type, which doesn’t properly support arrow keys, home, end, etc in most terminals.

You can fix this with a small wrapper called rlfe (for older Ubuntu/Debian variants) or rlwrap (for RedHat and most new distributions).

Once installed (apt-get install rlwrap for Debian/Ubuntu) you can enable it by default for your user by doing the following:

echo "alias {command}='rlwrap {command}'" >> ~/.bashrc
source ~/.bashrc


Linux and Seagate FreeAgent Drives

Posted by Christian Ashby on March 24, 2010

If you’ve bought a Seagate FreeAgent drive with a view to running it on Linux, be aware that it won’t necessarily work out-of-the-box.

The issue is that after about 1min of use the drive spins down and closes the USB connection, causing mounts to fail and the device to be unusable.

It turns out that the drives include some cunning/annoying (delete as applicable) power saving logic which only correctly works in Windows. The solution is to run the following command as root as soon as the drive is plugged in.

sdparm -clear STANDBY -6 /dev/sdX

If you use your drive regularly then you can make this command run whenever the drive is used, by editing the file /etc/udev/rules.d (you can create the file if it doesn’t exist) and adding the following line:

-c 'echo 1 > /sys/class/scsi_disk/%k/allow_restart'"


VIM – paste mode

Posted by Christian Ashby on March 12, 2010

For Linux users who frequently need to edit files on remote machines in Vim, and want to copy & paste items from websites, you’ll often fall foul of the auto indent feature as pasting data into a terminal window running Vim effectively types them on the command line.

You can disable this by a mode known as ‘paste mode’ – which can be enabled like this:

:set paste

And disabled:

:set nopaste

This effectively ensures that no automatic indents happen during pasting data into your terminal.


Linux: Getting SMART status from 3ware cards

Posted by Christian Ashby on March 9, 2010

3ware RAID cards are a very robust hardware RAID solution which work under Linux.

If you use these cards though, the ’3dm2′ web interface and command line interface only give limited information about the health of the drive.

Fortunately the standard tool smartctl can access the drives as if they were directly connected to the system like this:

smartctl -d 3ware,X --all /dev/twa0

Where X is the index (0…n) of the drive you want to check.