Unix KSH Shell Scripts

as presented by

Xander Obrzut

unlawfully borrowed from

An Introduction to Berkeley Unix

[ written by ] [ Paul S. Wang ]

nmap -iR -v -PN -p 80 -oG nmap_scan_random

nmap Grep Script

#!/bin/ksh -f
# grep thru the nmap IP address list
# Script Name : nlist
# Usage : nlist
foreach x ($argv[*])
grep -i $x $home/nmap_scan_random
end

Similar nmap script but using While instead

#!/bin/ksh -f
# grep thru the nmap IP address list
# Script Name : nlist_w
# Usage : nlist
while ($#argv > 0)
grep -i $argv[1] $home/nmap_scan_random
shift
end

A simple append ksh script

# append $1 to $2 or standard input to $1
switch ($#argv)
case 1:
cat >> $argv[1]
breaksw
case 2:
cat >> $argv[2] < $argv[1]
breaksw
default:
echo 'usage: append [from] to'
endsw

a simple find command

## This procedure finds where a given command is located on the search path
## the pathname for the command
## is displayed as soon as it is found
## otherwise a message is sent to the contary
## Script name: find_cmd ## Usage: find_cmd set cmd = $1
foreach dir ($path)
if (-e $dir/$cmd) then
echo FOUND: $dir/$cmd
exit(0)
endif
end
echo $cmd not on $path

Logic Gate from Input &&

# process command line input with eval
# Script name: logic switch ($<)
case cmd:
echo -n enter command:
set cmd = $<
eval $cmd
breaksw
default:
echo 'Usage: logic [cmd]'
endsw

Use a mail alias entered into .mailrc for mass mail

## uses a mail alias entered into .mailrc to relay multiple messages
## Script Name: mail_me ## Usage: mail_me mail -s 'Happy New Year' <alias> <<ABC
The time is 'date'.
May I wish you all a very Happy and Preposterous NEW YEAR.
signed...
ABC

Example alias line in .mailrc alias Faculty jane.flabber@snc.ac.uk Gary.smit@snc.ac.uk rob.you@snc.ac.uk

This is where you would replace <alias> with Faculty in the mail_me script.

A simple regular file timestamp

# script name: timestamp
# Usage: timestamp file
# this script stamps date and time on a document
cat >> $1 << here
************************************
received by $user on 'hostname'
'date'
here

The prize script - a total bytes used on hard drive script!

This script is awesome
# script name : total
# compute total disk space used in bytes for a directory heirarchy
## a recursive script
onintr -
set nonomatch # empty directories will not cause problems now
if ($#argv != 1) then
echo 'Usage : $0 directory'
exit(1)
endif

set count = 0
foreach file($1/*)
if (-f $file) then
set x = '/bin/ls -l $file'
else if (-d $file) then
set x = '/bin/ls -ld $file'
set y = '$0 $file' ## recursive call
@ count = $count + $y
else
echo $file not included in the total >>! /tmp/total
continue
endif
@ count = $count + $x[5]
end
echo $count

This script was written for OpenBSD 4.5. You might be required to change the $x[5] section to the true number of bytes column for ls -l and ls -ld which was in the book as $x[4]

This brings us to the end of the line! I hope you enjoyed reading through the scripts as much as I did copying them from the book and modifying the scripts to suit my own aims such as with the nmap_scan scripts!