Last active
November 20, 2015 17:43
-
-
Save forestbaker/7ae771f446efd0da17f1 to your computer and use it in GitHub Desktop.
Oldies but Goodies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# NAME | |
# DirCmp - compare the files in two directories | |
# | |
# SYNOPSIS | |
# DirCmp [-v] [dir1] dir2 | |
# | |
# DESCRIPTION | |
# This command compares the files in two directories and | |
# lists the files that are not the same. There will be | |
# three separate lists for: | |
# | |
# 1. Files not in the first directory, but in the second | |
# 2. Files not in the second directory, but in the first | |
# 3. Files in both directories, but not the same | |
# | |
# -v Verbose option. This option prints the lines that | |
# are different rather than just the name of the | |
# file when the file is in both directories, but the | |
# files are not the same. | |
# | |
# RETURN VALUE | |
# 0 The directories are the same | |
# 1 Usage error or abnormal termination | |
# 2 The directories are not the same | |
# | |
############################################################ | |
CMDNAME=`basename $0` | |
USAGE="Usage: $CMDNAME [-v] [dir1] dir2" | |
CURDIR=`pwd` # Current directory | |
DIR1= # Source directory | |
DIR2= # Target directory | |
DIR1_FILES=/tmp/files1.$$ # Files in dir1 | |
DIR2_FILES=/tmp/files2.$$ # Files in dir2 | |
ALL_FILES=/tmp/allfiles.$$ # Files in dir1 or dir2 | |
COMMON_FILES=/tmp/comfiles.$$ # Files in dir1 and dir2 | |
TMP=/tmp/tmp.$$ # Temporary file | |
FOUND=FALSE # Differences found? | |
FIRST= | |
VERBOSE=FALSE | |
trap 'rm -f /tmp/*.$$; exit 1' 1 2 3 15 | |
# | |
# Parse the command options. | |
# | |
while : | |
do | |
case $1 in | |
-v) VERBOSE=TRUE | |
shift | |
;; | |
--) shift | |
break | |
;; | |
-*) echo "$USAGE" 1>&2 | |
exit 1 | |
;; | |
*) break | |
;; | |
esac | |
done | |
# | |
# Get command line arguments. | |
# | |
if [ $# -eq 1 ]; then | |
DIR1="." | |
DIR2="$1" | |
elif [ $# -eq 2 ]; then | |
DIR1="$1" | |
DIR2="$2" | |
else | |
echo "$USAGE" 1>&2 | |
exit 1 | |
fi | |
# | |
# Check the directories. | |
# | |
if [ ! -d $DIR1 ]; then | |
echo "$DIR1 is not a directory." 1>&2 | |
exit2 | |
fi | |
if [ ! -d $DIR2 ]; then | |
echo "$DIR2 is not a directory." 1>&2 | |
exit2 | |
fi | |
# | |
# Find the files to compare. | |
# | |
cd $DIR1 | |
find . \( -type f -o -type l \) -print | sort >$DIR1_FILES | |
cd $CURDIR | |
cd $DIR2 | |
find . \( -type f -o -type l \) -print | sort >$DIR2_FILES | |
cd $CURDIR | |
# | |
# Build a list of all files. | |
# | |
cat $DIR1_FILES $DIR2_FILES | sort | uniq >$ALL_FILES | |
cat $DIR1_FILES $DIR2_FILES | sort | uniq -d >$COMMON_FILES | |
# | |
# Print the files that are in dir2, but not in dir1. | |
# | |
cat $DIR1_FILES $ALL_FILES | sort | uniq -u >$TMP | |
if [ -s $TMP ]; then | |
FOUND=TRUE | |
echo "" | |
echo "Files missing from $DIR1:" | |
for f in `cat $TMP` | |
do | |
f=`expr $f : '..\(.*\)'` | |
echo " $f" | |
done | |
fi | |
# | |
# Print the files that are in dir1, but not in dir2. | |
# | |
cat $DIR2_FILES $ALL_FILES | sort | uniq -u >$TMP | |
if [ -s $TMP ]; then | |
FOUND=TRUE | |
echo "" | |
echo "Files missing from $DIR2:" | |
for f in `cat $TMP` | |
do | |
f=`expr $f : '..\(.*\)'` | |
echo " $f" | |
done | |
fi | |
# | |
# Print the files that are in dir1 and dir2, but are not | |
# the same. | |
# | |
FIRST=TRUE | |
for f in `cat $COMMON_FILES` | |
do | |
cmp -s $DIR1/$f $DIR2/$f | |
if [ $? -ne 0 ]; then | |
FOUND=TRUE | |
f=`expr $f : '..\(.*\)'` | |
if [ "$FIRST" = "TRUE" ]; then | |
FIRST=FALSE | |
echo "" | |
echo "Files that are not the same:" | |
fi | |
if [ "$VERBOSE" = "TRUE" ]; then | |
echo "" | |
echo "File: $f" | |
diff $DIR1/$f $DIR2/$f | |
else | |
echo " $f" | |
fi | |
fi | |
done | |
rm -f /tmp/*.$$ | |
if [ $FOUND = TRUE ]; then | |
exit 2 | |
else | |
echo "The directories are the same." | |
exit 0 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# NAME | |
# findcmd - search for a command | |
# | |
# SYNOPSIS | |
# findcmd command | |
# | |
# DESCRIPTION | |
# This command searches the directories listed in the | |
# PATH variable for the command. It makes a reasonable | |
# attempt to find the same command as would be found by | |
# the shell, except that it does not find functions or | |
# built-in commands: | |
# | |
# 1. If the command name contains a /, the PATH variable | |
# is not used. | |
# 2. The directories in the PATH variable are searched in | |
# order, from left to right. | |
# 3. Files without execute access are ignored even if the | |
# file name matches the command name. | |
# 4. The search concludes when the first match is found. | |
# | |
# RETURN VALUE | |
# 0 Command was found | |
# 1 Usage error | |
# 2 Command was not found | |
# | |
############################################################ | |
CMDNAME=`basename $0` | |
if [ $# -ne 1 ]; then | |
echo "Usage: $CMDNAME command" 1>&2 | |
exit 1 | |
fi | |
FOUND=FALSE | |
COMMAND=$1 | |
case $COMMAND in | |
*/* ) if [ -x "$COMMAND" -a ! -d "$COMMAND" ]; then | |
echo "$COMMAND" | |
FOUND=TRUE | |
fi | |
;; | |
* ) IFS=: | |
for dir in `echo "$PATH" | | |
sed -e 's/^:/.:/' \ | |
-e 's/::/:.:/g' \ | |
-e 's/:$/:./'` | |
do | |
if [ -x "$dir/$COMMAND" -a ! \ | |
-d "$dir/$COMMAND" ] | |
then | |
echo "$dir/$COMMAND" | |
FOUND=TRUE | |
break | |
fi | |
done | |
;; | |
esac | |
if [ "$FOUND" = "FALSE" ]; then | |
echo "$COMMAND not found" | |
exit 2 | |
else | |
exit 0 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# NAME | |
# findstr - recursively search for a string | |
# | |
# SYNOPSIS | |
# findstr [-iv] string [filename] | |
# | |
# DESCRIPTION | |
# This command searches the files in the current | |
# directory and its subdirectories for the string. The | |
# name of each file that contains the string is listed. | |
# | |
# The string may be a simple string or it may be any | |
# regular expression accepted by the grep command. If | |
# the string contains whitespace or any other | |
# metacharacter, it must be quoted. | |
# | |
# The search can be restricted to files with a particular | |
# name by specifying the file name parameter. This | |
# parameter may contain wildcard characters to restrict | |
# the search to file names that match a pattern, but the | |
# file name must be quoted so that the wildcard | |
# characters can be processed inside this command file | |
# rather than being expanded into file names on the | |
# command line. | |
# | |
# -i Ignore the case of the string. | |
# | |
# -v Verbose; list the lines that contain the string. | |
# Without this option, only the names of the files | |
# containing the string will be printed. | |
# | |
# RETURN VALUE | |
# 0 Successful completion | |
# 1 Usage error | |
# | |
############################################################ | |
CMDNAME=`basename $0` | |
USAGE="Usage: $CMDNAME [-iv] string [filename]" | |
STRING= # String to search for | |
FILENAME= # Name of the files to check | |
I= # Option for grep; Ignore case | |
L=-l # Option for grep; List names only | |
# | |
# Parse command options. | |
# | |
if [ "$OPTIND" = 1 ]; then | |
while getopts iv OPT | |
do | |
case $OPT in | |
i) I=-i # Ignore case | |
;; | |
v) L= # Verbose | |
;; | |
\?) echo "$USAGE" 1>&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift `expr $OPTIND - 1` | |
else | |
USAGE="Usage: $CMDNAME [-i][-v] string [filename]" | |
while : | |
do | |
case $1 in | |
-i) I=-i # Ignore case | |
shift | |
;; | |
-v) L= # Verbose | |
shift | |
;; | |
--) shift | |
break | |
;; | |
-*) echo "$USAGE" 1>&2 | |
exit 1 | |
;; | |
*) break | |
;; | |
esac | |
done | |
fi | |
# | |
# Make sure the number of parameters is reasonable. | |
# | |
if [ $# -lt 1 -o $# -gt 2 ]; then | |
echo "$USAGE" 1>&2 | |
exit 1 | |
fi | |
STRING=$1 | |
FILENAME=${2:-"*"} | |
find . \( -type f -o -type l \) -name "$FILENAME" -print | | |
xargs -e grep $I $L -- "$STRING" /dev/null | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# SYNOPSIS | |
# MailPkg address package file ... | |
# | |
ADDRESS=$1 | |
PACKAGE=$2 | |
shift 2 | |
tar -cf /tmp/package.$$ $* | |
mkdir /tmp/split.$$ | |
cd /tmp/split.$$ | |
compress /tmp/package.$$ | | |
uuencode $PACKAGE.tar.Z | | |
split -1000 | |
PARTNUM=1 | |
MESSAGE=/tmp/message.$$ | |
set x* | |
TOTAL=$# | |
while [ $# -gt 0 ] | |
do | |
cat <<_EOF >$MESSAGE | |
This is part $PARTNUM of $TOTAL of a compressed and | |
uuencoded tar file. | |
- File: $PACKAGE.tar.Z | |
- Part: $PARTNUM | |
----- Cut Here ----- | |
EOF | |
cat $1 >>$MESSAGE | |
echo "----- Cut Here -----" >>$MESSAGE | |
mail $ADDRESS <$MESSAGE | |
shift | |
PARTNUM=`expr $PARTNUM + 1` | |
done | |
rm /tmp/package.$$ | |
rm /tmp/message.$$ | |
rm -rf /tmp/split.$$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# NAME | |
# ptree - print a process tree | |
# | |
# SYNOPSIS | |
# ptree [-n] [pid] | |
# ptree [-n] [pid] [level] [datafile] | |
# | |
# DESCRIPTION | |
# This command will print the process tree for the | |
# process identified by pid. When called by the user, | |
# only the first format shown should be used. If the | |
# process ID (pid) is not passed, process 1 is assumed. | |
# | |
# The second format of this command is only used when | |
# this command calls itself recursively to print the next | |
# level of the process tree. | |
# | |
# -n Do not recurse | |
# | |
# RETURN VALUE | |
# 0 Successful completion | |
# 1 Usage error | |
# | |
############################################################ | |
PATH=$PATH:`dirname $0` | |
. SystemType.sh | |
CMDNAME=`basename $0` | |
USAGE="Usage: $CMDNAME [-n] [pid]" | |
RECURSIVE=TRUE # List processes recursively | |
PROCESS= # PID of the starting process | |
LEVEL= # Indentation level (num parents) | |
DATAFILE= # Reformatted output from ps | |
PSFILE=/tmp/ps.$$ # Output from the ps command | |
PS_OPTS= # Options for the ps command | |
OLD_IFS=$IFS # Original value of IFS variable | |
SYSTEM=`SystemType` # String identifying the system | |
# | |
# Temporaries | |
# | |
PID= # Process ID | |
PPID= # Process ID of the parent process | |
OWNER= # Owner of the process | |
NAME= # Name of the process | |
LINE= # Line from the data file | |
OUTLINE= # Line of output | |
INDENT= # Column number where line begins | |
trap 'rm -f /tmp/*.$$; exit 1' 1 2 3 15 | |
FillLine() { | |
# | |
# SYNOPSIS | |
# FillLine line column | |
# | |
_LINE="$1" | |
_COLUMN=$2 | |
_LEN=`expr "$_LINE" : '.*'` | |
while [ $_LEN -lt $_COLUMN ] | |
do | |
_LINE="$_LINE " | |
_COLUMN=`expr $_COLUMN - 1` | |
done | |
echo "$_LINE" | |
} | |
while : | |
do | |
case $1 in | |
-n) RECURSIVE=FALSE | |
shift | |
;; | |
--) shift | |
break | |
;; | |
-*) echo "$USAGE" 1>&2 | |
exit 1 | |
;; | |
*) break | |
;; | |
esac | |
done | |
# | |
# Make sure the number of parameters is reasonable. | |
# | |
if [ $# -eq 0 ]; then | |
PROCESS=1 | |
LEVEL=0 | |
DATAFILE=/tmp/ptree.$$ | |
elif [ $# -eq 1 ]; then | |
PROCESS=$1 | |
LEVEL=0 | |
DATAFILE=/tmp/ptree.$$ | |
elif [ $# -eq 3 ]; then | |
PROCESS=$1 | |
LEVEL=$2 | |
DATAFILE=$3 | |
else | |
echo "$USAGE" 1>&2 | |
exit 1 | |
fi | |
if [ "$LEVEL" = 0 ]; then | |
# | |
# Determine which options to use with the ps command. | |
# | |
case $SYSTEM in | |
SUNBSD | ULTRIX ) PS_OPTS="-auwx" ;; | |
* ) PS_OPTS="-ef" ;; | |
esac | |
# | |
# Build the data file. | |
# | |
rm -f $DATAFILE $PSFILE | |
ps -ef | sed '1d' | sort >$PSFILE | |
exec <$PSFILE | |
IFS= | |
while read LINE | |
do | |
IFS=$OLD_IFS | |
set $LINE | |
OWNER=$1 | |
PID=$2 | |
PPID=$3 | |
# | |
# Determine column where the process name begins. | |
# | |
case $SYSTEM in | |
AIX | HP | SGI | SOLARIS ) COL=48 ;; | |
SUNBSD | DECOSF ) COL=57 ;; | |
ULTRIX ) COL=51 ;; | |
* ) echo "Unexpected system type." 1>&2 | |
exit 1 | |
;; | |
esac | |
LINE=`echo "$LINE" | cut -c$COL-` | |
set dummy $LINE | |
shift | |
NAME=$1 | |
echo $PID $PPID $OWNER $NAME >>$DATAFILE | |
IFS= | |
done | |
IFS=$OLD_IFS | |
fi | |
# | |
# Print the current process. | |
# | |
INDENT=`expr $LEVEL \* 2` | |
OUTLINE=`FillLine "" $INDENT` | |
LINE=`grep "^$PROCESS " $DATAFILE` | |
set $LINE | |
OUTLINE="$OUTLINE $1" | |
OUTLINE=`FillLine "$OUTLINE" 30` | |
OUTLINE="$OUTLINE $3 $4" | |
echo "$OUTLINE" | |
if [ "$RECURSIVE" = "TRUE" ];then | |
LEVEL=`expr $LEVEL + 1` | |
while read LINE | |
do | |
set $LINE | |
# | |
# For every process that is a child of the | |
# current process, invoke this command ($0) | |
# recursively. | |
# | |
if [ "$2" = "$PROCESS" ]; then | |
$0 $1 $LEVEL $DATAFILE | |
fi | |
done <$DATAFILE | |
fi | |
rm -f /tmp/*.$$ | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright 1995, by Hewlett-Packard Company | |
# | |
# The code in this file is from the book "Shell Programming | |
# Examples" by Bruce Blinn, published by Prentice Hall. | |
# This file may be copied free of charge for personal, | |
# non-commercial use provided that this notice appears in | |
# all copies of the file. There is no warranty, either | |
# expressed or implied, supplied with this code. | |
# | |
# NAME | |
# Shar - create a shell archive | |
# | |
# SYNOPSIS | |
# Shar file [...] | |
# | |
# DESCRIPTION | |
# This command creates a shell archive that contains the | |
# files listed on the command line. The shell archive | |
# is written to the standard output. To unpack the | |
# archive, enter the following command, where archive is | |
# the name of the shell archive. | |
# | |
# sh archive | |
# | |
# RETURN VALUE | |
# 0 Successful completion | |
# 1 Usage error | |
# | |
############################################################ | |
CMDNAME=`basename $0` | |
if [ $# -lt 1 ]; then | |
echo "Usage: $CMDNAME file ..." 1>&2 | |
exit 1 | |
fi | |
echo "#!/bin/sh" | |
echo "# This is a shell archive; to unpack:" | |
echo "# 1. Remove everything before the \"#!/bin/sh\"." | |
echo "# 2. Save the rest of the archive." | |
echo "# 3. Execute the archive by entering \"sh archive\"." | |
echo "#" | |
echo "# This archive contains the following files:" | |
echo "#" | |
for FILE | |
do | |
echo "# $FILE" | |
done | |
for FILE | |
do | |
echo "" | |
echo "if [ -f $FILE ]; then" | |
echo " echo The file $FILE already exists." | |
echo "else" | |
echo " echo Extracting $FILE" | |
echo " sed 's/^X//' >$FILE <<\EOF" | |
sed 's/^/X/' <$FILE | |
echo "EOF" | |
echo "fi" | |
done | |
echo "exit 0" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment