Unix Snippets

Introduction

Here's a few snippets of Unix Code. None of the code is enough to form a program on its own but can be included in programs. Whenever I solve a tricky little problem, I'll try to remember to put the solution here.

ksh History

ksh(1) command line history is associated with a user, not a terminal. If you login to Unix on several X-terminals, they share a common history. I find this confusing and prefer each terminal to have its own history, like when using the bash(1) shell.

You can give each terminal its own command line history by adding the following code to one of your .rc files for your user:


export HISTFILE=$HOME/.histfile.`tty | sed s+.*/++`

Package request file

I've not found a man page for the packing request file. So, I've written some notes.

Here's an example of setting up the BASEDIR variable.


# Installation request script.
#
trap 'exit 3' 15

# Find BASEDIR
#
BASEDIR=`ckpath \
 -d $BASEDIR \
 -e "Please enter the name of an existing directory" \
 -h "Enter the name of the directory where the package should be installed" \
 -p "Enter the path of the package base directory" `
 -a -k $$ -o -r -w -x -y

# Write variables to output file
#
echo BASEDIR="${BASEDIR}" >>$1

# All worked fine
#
exit 0
It's worth remembering:

The request file is documented at http://docs.sun.com/db/doc/817-0406/6mg76stev?a=view.

Yesterday's Date

How do you determine yesterday's date within a shell script?

I've seen references to date(1) programs that take additional parameters, such as:

date -d '-1 day' +%Y%m%d
or:
date --date=yesterday +%Y%m%d
But I"ve not encountered these variants of date(1) when I've needed them.

I've used the following trick:

tz=$TZ
date | grep BST >/dev/null 
if [[ $? -eq 0]] 
then 
 export TZ=$TZ+23 
else 
 export TZ=$TZ+24 
fi 
YESTERDAY=`date +%Y%m%d`
export TZ=$tz
This works in ksh on Solaris when my locale has set the TZ to GB. The trick sets the timezone 24 hours ahead, where it is still yesterday.
Author: Stewart Smith
Last update: 31st October 2005
©Copyright: Pentagon Computer Consultants Ltd 2004-5.