NEW PAPER TOYS!
Available to Download Now!

Bash Console Colours for C++

Here’s a little header file for C/C++ that I knocked up to make colouring console output a little easier. Not all of these colour codes are supported by every console emulator, but if you stick to the primary colours, you should be fine.

File: shellColours.h
//// Foreground Colours ////
# define SH_FG_BLACK         "\033[0;30m"
# define SH_FG_BLUE         "\033[0;34m"
# define SH_FG_GREEN         "\033[0;32m"
# define SH_FG_CYAN         "\033[0;36m"
# define SH_FG_RED         "\033[0;31m"
# define SH_FG_PURPLE         "\033[0;35m"
# define SH_FG_YELLOW         "\033[0;33m"
# define SH_FG_LIGHT_GREY     "\033[0;37m"
# define SH_FG_DARK_GREY     "\033[1;30m"
# define SH_FG_LIGHT_BLUE     "\033[1;34m"
# define SH_FG_LIGHT_GREEN     "\033[1;32m"
# define SH_FG_LIGHT_CYAN     "\033[1;36m"
# define SH_FG_LIGHT_RED     "\033[1;31m"
# define SH_FG_LIGHT_PURPLE    "\033[1;35m"
# define SH_FG_LIGHT_YELLOW     "\033[1;33m"
# define SH_FG_WHITE         "\033[1;37m"

//// Background Colours ////
# define SH_BG_BLACK         "\033[0;40m"
# define SH_BG_BLUE         "\033[0;44m"
# define SH_BG_GREEN         "\033[0;42m"
# define SH_BG_CYAN         "\033[0;46m"
# define SH_BG_RED         "\033[0;41m"
# define SH_BG_PURPLE         "\033[0;45m"
# define SH_BG_YELLOW         "\033[0;43m"
# define SH_BG_LIGHT_GREY     "\033[0;47m"
# define SH_BG_DARK_GREY     "\033[1;40m"
# define SH_BG_LIGHT_BLUE     "\033[1;44m"
# define SH_BG_LIGHT_GREEN     "\033[1;42m"
# define SH_BG_LIGHT_CYAN     "\033[1;46m"
# define SH_BG_LIGHT_RED     "\033[1;41m"
# define SH_BG_LIGHT_PURPLE    "\033[1;45m"
# define SH_BG_LIGHT_YELLOW     "\033[1;43m"
# define SH_BG_WHITE         "\033[1;47m"

//// Others ////
# define SH_DEFAULT         "\033[0m"
# define SH_UNDERLINE         "\033[4m"
# define SH_BLINK         "\033[5m"
# define SH_INVERSE         "\033[7m"
# define SH_CONCEALED         "\033[8m"

And here’s how to use it. Note that you’ve got to set the colours back to default at the end of the sequence.

File: shellColoursTest.cpp
# include "../shellColours.h"
# include
using namespace std;

int main (){
cout < < SH_FG_BLUE <<"Foreground Blue" << SH_DEFAULT << endl;
cout << SH_BG_GREEN <<"Background Green" << SH_DEFAULT << endl;
cout << SH_UNDERLINE <<"Text Underlined" << SH_DEFAULT << " Back To Normal Text" << endl;
}

Crontab Debugging

Sometimes, a script which works perfectly fine when run from the shell, fails when run as a cron job. It’s often quite difficult to debug these problems, but to make the task a little easier, try this.

Edit the crontab file with
crontab -e

Add this line for your script (obviously change the script path and username)
*/5 * * * * /usr/bin/myscript >~username/crontab.out 2>~username/crontab.err

This will run your script every 5 minutes, and send outputs to two files. Standard output will go to contab.out and errors will go to crontab.err.

If you want to use a different editor run the following, then start over.
EDITOR=/path/to/editor
export EDITOR

BASH Variable Scope in While Loop

Here’s an annoying little foible of BASH shell scripting.

I’ve been trying to writea script to test authorization against a list of servers, and fire off an email if any of the servers doesn’t respond. The lis tof servers is in a text file, and I’ve got a while loop to itterate through, testing line by line. If any of the tests fails then, I’d change a flag variable from its default value of “FALSE” to “TRUE”. After the loop I can test the value of the flag variable, and send an email accordingly.

The problem I was having is that the variable scope didn’t seem to extend beyond the while loop, so the final check always showed that the flag was set to FALSE, even if it was changed within the loop.

My original script looked something like this…

SENDMAIL=
grep "^server:" servers.file | while read line; do
if [ testauth() ]; then
SENDMAIL="TRUE"
fi
done
if [ SENDMAIL ]; then
sendMail()
fi

The problem is that, I’ve piped my file through grep and in to the while loop. Because of the way the shell handles piped commands, the variable scope is limited to within the while loop. To get around this problem, you have to remove the pipe in to the while loop. In this case I chose to output to a temp file then read it in, but you can also pipe to a variable, then loop over the variable.

SENDMAIL=
grep "^server:" servers.file > servers.tmp
while read line; do
if [ testauth() ]; then
SENDMAIL="TRUE"
fi
done < servers.tmp
if [ SENDMAIL ]; then
sendMail()
fi

Xming the Xmerciless

For a while now I’ve been looking for an X server for Windows, so that I can start X applications from within a secure shell. Up until now Hummingbird has a been a fair solution, but I’ve never been too impressed with it. I’d never been able to get it working with PuTTY, so I have had to revert to Hummingbird’s mediocre terminal client when I needed to run X apps. And when it’s running it sits in the way on my (already over cluttered) task bar.

But not any more! I’ve found Xming, which is brilliant. It’s hardly new, but it’s new to me, so I’m excited. It works wonderfully with PuTTY. It starts when I log in and runs in the background with nothing but a system tray icon. It does all sorts of other X’y stuff, but all I want from my X server, is for it to stay out the way until needed. Hummingbird didn’t, Xming does. Lovely stuff.