Category Archives: Linux

Calling mysqldump in Python

Python is a fantastic tool to know, and despite being a beginner I find myself using it more and more for everyday tasks. Bash is great for knocking together quick scripts, but when you want to something a little more complex such as interfacing with APIs or other systems over a network, you really need a more fully-featured programming language.

The topic of this post, however, is the kind of task that bash is perfect for. Thanks to mysqldump, a database backup script can be written in a few lines and dump/restores are easily automated. So why on earth would we do this in Python?
Continue reading

How not to program Bash

Came across this gem today:
[shell]
COMP=$1

if [ -e $COMP ]; then
echo “Please supply a competition_id”
exit
fi
[/shell]

On first read it looks backwards – “if exist $COMP then exit”, and the programmer clearly wanted to do the exact opposite. However it actually works as desired. Mostly.

If no argument is supplied, $COMP will be a blank string, i.e. “”. Thus [ -e $COMP ] will parse to [ -e ], which returns 0 (effectively true). It’s basically like saying “does nothing exist”.

This is because the -e argument tests whether a file exists. So if an argument is supplied, [ -e $COMP ] will return 1 (effectively false), which does what the programmer intended UNLESS the argument supplied is a valid file path.

In this case a number was expected, so sure it’s unlikely to fail in this way, but it’s still an incredibly bad way to test if an argument is set. Not to mention confusing to read!

The correct way to test this by the way would be to use -z, which simply tests if a string’s length is zero:
[shell]
COMP=$1

if [ -z “$COMP” ]; then
echo “Please supply a competition id”
exit
fi
[/shell]

Or better still, use getopts. For more info run ‘man test’ from a bash terminal.

Creating samba share in Nautilus: ‘net usershare’ returned error 255

I was having this problem on Ubuntu 12.04 (precise), but most of the Google results pointed to a bug in Hardy. However there are other causes of this problem.

In my case it was a previously-created share with a different user ID – Nautilus couldn’t create the share because there was already a share file with the same name owned by a different user.

The directory is /var/lib/samba/usershares. You should already have write access assuming you’re a member of the sambashare group (which the gui should handle for you), so all that remains to be done is remove the offending share with the same name as the one you’re trying to create.

alex@al4:~$ cd /var/lib/samba/usershares/
alex@al4:/var/lib/samba/usershares$ ls -lah
total 16K
drwxrwx--T 2 root       sambashare 4.0K Jul 25 12:33 .
drwxr-xr-x 5 root       root       4.0K May  1 10:40 ..
-rw-r--r-- 1 2046297271 2046296576  142 Oct 25  2011 music
-rw-r--r-- 1 2046297271 2046296576  128 Feb  7 17:13 videos
alex@al4:/var/lib/samba/usershares$ sudo rm music
[sudo] password for alex:
alex@al4:/var/lib/samba/usershares$ sudo rm videos
alex@al4:/var/lib/samba/usershares$

After doing the above, Nautilus was able to recreate the shares without trouble.

Photo workflow script

After trying several photo management tools I’ve concluded that the best way to manage a photo library is the simplest – copy the files yourself and name the directory.

I also often shoot in RAW+JPG mode on my camera and like to keep the files separate to make viewing saner, so under each event directory I will have a RAW and JPG directory as well.

After downloading, the first step is always to delete the photos that didn’t come out. With both raw and jpeg files to delete this process is a bit of a pain, especially when they’re in separate directories, so I wrote a script that deletes the raw file when the jpg is missing. That way I can simply browse through the JPG directory with Eye of Gnome, delete the photos I don’t want, and the script handles cleanup of the RAW directory.

https://github.com/al4/scripts/blob/master/bash/rawdel.sh

The code is below but you should use the github link above in case it gets improved at some point in the future.

[shell]
#!/bin/bash

# rawdel.sh, a photo workflow script to delete raw files when the jpg has been removed
# By Alex Forbes
#
# I frequently shoot in RAW+JPG mode and when downloading from the camera I like to separate
# the raw and jpg files into separate directories. I then go through the jpg directory and
# delete the rejects. It is a pain to have to manually delete the corresponding raw files as
# well, so I wrote a script to do it for me.
#
# It simply removes RAW files from a directory when the corresponding JPG file has been removed.

# Set these
rawextn=”CR2″ # raw file extension (Canon is CR2)
rawdir=”./RAW” # directory where raw files reside
jpgdir=”./JPG” # directory where jpg files reside
# rawdir and jpgdir can be the same

# Working variables, leave as-is
list=”” # list of files that have been deleted
rawlist=”” # the list of raw files that we will delete
filecount=”” # number of files we will delete

# Operate on each raw file
for f in $(ls -1 $rawdir/*.$rawextn); do
# Corresponding JPG file is:
jpgfile=$(basename $f | sed “s/\.$rawextn$/.JPG/”)

# If this JPG file doesn’t exist
if [ ! -f $jpgdir/$jpgfile ]; then
# Add to our list of files that have been deleted
list=$(echo -e “${jpgfile} ${list}”)
fi
done

# Convert jpg filenames back to corresponding raw filenames
rawlist=$(echo ${list} | sed ‘s/\.JPG$/.CR2/g’)
filecount=$(echo -e ${rawlist}| awk ‘END{print NF}’)

if [ $filecount == 0 ]; then
echo “No files to delete”
exit 0
fi

echo -e “About to remove $filecount files:\n${rawlist}”
read -p “Continue? [Y/N] ” prompt

if [[ $prompt = “Y” || $prompt = “y” ]]; then
# Delete all files in the list
for f in ${rawlist}; do
rm -v $rawdir/$f
done
exit 0
else
echo -e “\nAborted.”
exit 1
fi
[/shell]

Intel wifi led blinking AGAIN on Ubuntu 12.04

I previously posted about this on previous versions of Ubuntu, but despite updating the instructions for 11.10 the instructions are once again obsolete. It seems Intel changes the name of its wifi kernel module every release…

On my Dell E4300 with “Intel Corporation WiFi Link 5100” (as reported by lspci), the module name is now “iwlwifi”. This means the kernel options you add to /etc/modprobe.d should be against this module rather than iwlcore (11.04) or iwlagn (11.10).

So the instructions once again:

$ sudo -i
# echo 'options iwlwifi led_mode=1' >> /etc/modprobe.d/wlan.conf
# modprobe -r iwlwifi && modprobe iwlwifi

Bear in mind that the second line removes the wifi kernel module temporarily which will disconnect your wifi. It should automatically reconnect, if not reboot.

Slow desktop performance on Ubuntu 11.10 with nvidia graphics cards

11.10 has been a bit of a mixed bag. On the plus side it has Gnome 3, giving me a practical (and in my opinion superior) alternative to Unity. On the minus side I had upgrade glitches on both my work and personal machines, and they were unrelated issues! Might be wise to wipe and reinstall for this one (you did separate your home partition when you installed didn’t you :)).

Anyway after getting it working on my work PC (which has an 8400GS), the desktop was quite laggy in both Unity and Gnome3. It was still usable but I didn’t realise how bad it was until I went home and noticed how much smoother my laptop was, with its lowly 2009-era Intel integrated graphics…

The solution was to install the latest 285.05 Nvidia driver, but trust me when I tell you that you do not want the hassle of using the Nvidia installer from the Nvidia website.

It is much simpler to use the X Updates ppa.

So assuming you already have the default binary Nvidia driver installed and activated (nvidia-current), the quick command line solution to your performance woes should hopefully be:

sudo -i
add-apt-repository ppa:ubuntu-x-swat/x-updates
apt-get update && apt-get upgrade
reboot

You should notice an update for the nvidia-current package being installed.

That wasn’t too bad was it? :)

How not to troubleshoot an unexplained server reboot

We asked our provider to investigate why one of our servers rebooted last night. In the process they accidentally rebooted it again… this is root’s bash_history just before it happened, note line 971:

  954  2011-08-17_15:10:39 sar -q
  955  2011-08-17_15:10:59 sar -q|less
  956  2011-08-17_15:11:09 sar -r|less
  957  2011-08-17_15:11:24 last -x|less
  958  2011-08-17_15:11:49 history |grep -i shutd
  959  2011-08-17_15:11:21 history
  960  2011-08-17_15:11:32 date
  961  2011-08-17_15:13:52 cd /var/log/
  962  2011-08-17_15:13:53 ls
  963  2011-08-17_15:13:54 ls -lah
  964  2011-08-17_15:13:58 less audit/
  965  2011-08-17_15:14:04 less audit/audit.log
  966  2011-08-17_15:14:25 less secure
  967  2011-08-17_15:15:15 grep -v nagios secure | less
  968  2011-08-17_15:16:11 dmesg
  969  2011-08-17_15:17:57 sar -r
  970  2011-08-17_15:18:19 dmesg
  971  2011-08-17_15:18:30 dmesg | reboot
  972  2011-08-17_16:20:20 [LOGOUT]: xxxx     pts/2        2011-08-17 15:27 (xxx.xxx.xxx.xxx)

Continue reading

Stopping the Intel WiFi LED from blinking in Ubuntu

My Dell E4300 has an Intel 5100 wifi card and the led blinks constantly. I still don’t understand how Intel can consider blinking the wifi LED during data transfer to be a sensible default. For most people it blinks non-stop which is both uninformative and irritating.

Fortunately blogger Alex Cabal found a solution for Karmic, and his updated solution also works for Natty/11.04. It describes opening a text editor and pasting a couple of lines, however I’m much lazier so here’s the one-line version:

echo 'options iwlcore led_mode=1' >> /etc/modprobe.d/wlan.conf

Of course, the command above must be run as root (sudo -i), for some reason sudo gave me access denied. You also need to reboot… or you could just unload and reload the module:

modprobe -r iwlagn && modprobe iwlagn

The double ampersand just executes the next command if the previous one succeeded (exit status 0).

Update

As of Ubuntu 11.10 (kernel 3.0.0) the option has to be applied to the iwlagn module, options for iwlcore are ignored. Thus the full solution now becomes:

sudo -i
echo 'options iwlagn led_mode=1' >> /etc/modprobe.d/wlan.conf
modprobe -r iwlagn && modprobe iwlagn