[Gllug] Simple Bash Script

- Tethys tethys at gmail.com
Tue Jun 3 09:54:55 UTC 2008


On Tue, Jun 3, 2008 at 8:49 AM, John Edwards
<john at cornerstonelinux.co.uk> wrote:

> Getting rid of tempfiles is a little personnal mission of mine after
> seeing thousands of them generated on some systems. Not only are they
> messy if the script ends halfways through, but can also be a security
> risk if the name is not suitably random or the contents are not
> sanitised.

Temp files have their uses, and so long as you remove them with a trap
on exit, they won't leave a mess on your filesystem either:

tmpfile=$(mktemp -t mytmpfile.XXXXXXXXXXXXX)
trap "/bin/rm -f '$tmpfile'" 0 INT HUP QUIT TERM

> Apart from that the other thing that might be cleaned up is the maths
> evaluations. Rather than defining $TRUE and using bc to generate
> $RESULT, I wonder if it's possible to use something like:
>
> if [ $LOAD -gt $NOTIFY ]

The shell doesn't do floating point arithmetic. However, load averages
are always given to 2DP, so the standard trick is to remove the
decimal point, effectively multiplying the load by 100, and compare it
to a similarly multiplied threshold:

notify=600
load=$(sed 's/\.\(..\).*/\1/' /proc/loadavg)
if [ $load -gt $notify ]
then
	...
fi

Also, I might as well mention my pet peeve again: don't use upper case
variable names in shell scripts. By convention, shell variables should
be lower case, and environment variables should be upper case.

Tet

-- 
Perl is like vise grips. You can do anything with it but it is the
wrong tool for every job. -- Bruce Eckel
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list