Archive for December, 2010

h1

When is an int not an int?

December 16, 2010

One but, but only one, use of a type system is to lay out data structures in memory.
(This use is particularly relevant to systems programming and hence to C). So
why does C need a type that is not specific about its size (and int is only required to
be at least 16 bits long) in addition to its gamut of signed and unsigned longs, floats
and doubles.

Int is in fact a speed optimisation: it is whichever format is processed most quickly
in the opinion of a compiler. Thus it is a highly appropriate choice for use in loops,
given the proviso that it cannot be guaranteed to more than 16 bits. Equally, it would
be a poor choice for laying out memory structures that need to be a precise size.

Advertisements
h1

Shell Tips 7: The Input Field Separator Trick

December 7, 2010

There is an environment variable called IFS which can perform simple tokenisation on input. By defulat
IFS is set to whitespace, blanks, tabs etc.
OLDIFS=”$IFS” #Save original field separator
IFS=”,”
Line=”a,b,c,d” #Now separate on commas
let Count=0
for item in $Line
do
let Count=Count+1
echo “Item $Count = $item”
done
IFS=”$OLDIFS” #Restore original field separator

h1

Shell tips 3: Empty and unassigned shell variables

December 4, 2010

The x$foo trick
A shell variable without anything around it will disappear entirely, which can cause syntax errors. This tends to show
up in “if” statements. One, solution, which I dislike , is to write this sort of thing

if [ x$foo = x ]

which is actually a test that the variable is empty, not that is equal to x.

Better is

if [ "$foo" = "" ]

h1

Shell tips 6: Efficiency

December 4, 2010

Avoid launching binary processes. This can make a huge difference to the efficiency of script.
The commands you can type in at the prompt take two quite different forms: actual programmes (also known as binaries) and “shell builtins”, which are subroutines with in the shell. The difference, by design, is not obvious.
Historically, external binary programmes have been replaced by builtins with the same syntax so that scripts will continue to work. Study of the man page for your chose shell or use of the “type” command will tell you which is which. Wherever possible, use
builtins instead.

Especially avoid the binary command “expr” which evaluates arithmetic expressions.

Sum=`expr 2 + 2`

use the builtin “let” instead

let Sum=2+2.

“expr” is particualrly dangerous because it is often written into loops. It takes thousands of clock cycles (and some disk access) to launch “expr”, and only a few clock cycles to perform the calculation.

Avoid unnecessary executable launches using pipelines.

cat $MyFile | while read Line
do
echo $Line | tr '[a-z]' ['A-Z']
#The executable tr is called once per
#line to put Line into upper case
done

can be much more efficiently written


cat $MyFile | tr '[a-z]' '[A-Z]'

In particular, invocation of the executables grep, awk and sed can be avoided by performing pattern matching with the builtin,”case”, (but only for some of the instances)

cat $MyFile | while read Name
do
if [ $(echo $Name | grep -c "^Mr" ]
then
echo male
fi
if [ $(echo $Name | grep -c "^Mrs" ]
then
echo female
fi
if [ $(echo $Name | grep -c "^Miss" ]
then
echo female
fi
if [ $(echo $Name | grep -c "^Ms" ]
then
echo female
fi
done


can be much more efficiently written

cat $MyFile | while read Name
do
case $Name in
"Mrs *")) echo female ;;
"Miss *")) echo female ;;
"Ms *")) echo female ;;
"Mr *")) echo male ;;
esac
done

h1

Shell tips 5: Run external programmes non-interactive

December 4, 2010

Some external programmes, such as “fsck”
will pause for user input, which means a script using them cannot be run unattended. There is a usually
an options (such as “-y” for “yes to everything” to override that behaviour).

h1

Shell tips 4: Empty and unassigned variables

December 4, 2010

The x$foo trick
A shell variable without anything around it will disappear entirely, which can cause syntax errors. This tends to show
up in “if” statements. One, solution, which I dislike , is to write this sort of thing

if [ x$foo = x ]

which is actually a test that the variable is empty, not that is equal to x.
3.3 Better is

if [ "$foo" = "" ]

h1

Shell tips 1: Options

December 4, 2010

The shell has many options: two of them are particularly useful for programme development.
Although they can be set on invocation like other options, it is generally more useful to use the “set” command which
can be placed within a script.
In most cases these lines should be added at the top of the file
To make your life easier, add the line

set -u

to the beginning of you scripts. This is the rough equivalent of
PERL’s “use strict”. It will give you an error message if you
attempt to use an undefined variable. Undefined variables are
a common headache and cause all sorts of weird behaviour. For instance

if [ -f $MyFile ]

will evaluate to

if [ -f ]

if MyFile is unassigned, which is a *syntax error*.
Another example:

cat $MyFile


will *hang on inpu* if myfile is undefined, since that is what

cat

by itself does. And you don’t get any error message, the script just stops!

If you find you have mysterious hanging problems or othe puzzling
behaviour another global option you can use is

set -x

which basically prints each line (on stderr) as it is executed.
We do not recommend you leave
this switched on all the time.