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 file must be called request.
- The request
file must appear in the prototype file.
- The request file is called with one
argument – the name of the file to write.
- The ckpath
program sends a signal to the request script
($$) if the user
chooses to quit.
Hence the trap statement.
- The program should write a list of variable names and values to the file,
as if they are shell variables
(name=value).
- The request script exit code indicates whether
the script worked. 0 – success.
- The ckxxx programs are only available on
Solaris.
On other systems, other techniques are required to collect data from the user.
- The BASEDIR variable cannot be set from the
request file on all systems.
Other variables can.
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:
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.
|
|