shell script to append to a file

Liam R. E. Quin lee at sq.sq.com
Sun Nov 12 14:48:36 AEST 1989


Here's something I've been using for a few years now.  It was inspired by
the 4.1 BSD (I said a few years...) Mail command.

It is like
	cat >> file
except that the Mail-style ~ escapes work, so you can do
	~v
to go into vi (or $VISUAL), ~p to print, and ~? for a list of commands.

There is no man page because I didn't intend it to be a separate utility.
Seems to work on BSD and System V, although you might want to tweak the
default editors.  Don't use "emacs" for VIUSUAL, it is supposed to be an
editor that starts up quickly!

Lee

------ snip ----- snip ------ -------- cut here ------ cut here -----
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  bin/a
# Wrapped by lee at sq on Sat Nov 11 22:48:00 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'bin/a' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'bin/a'\"
else
echo shar: Extracting \"'bin/a'\" \(2804 characters\)
sed "s/^X//" >'bin/a' <<'END_OF_FILE'
X#! /bin/sh
X: use /bin/sh
X# put the : line first on System V, and the #! line first on BSD.
X#
X# a -- append to a (named) file, allowing tilde escapes
X# You have to name the file on the command line, as in
X#	a /tmp/socks.$$
X#
X# Liam Quin, 1982 (ubu!req)
X# Modified for Unisoft (Pixel) 1986 (anduk!russ)
X# Modified for System V (Honeywell Bull) 1988 (russ at anduk.co.uk)
X# Modified for 386/ix and Xenix 1989 (lee at anduk.co.uk)
X# SunOS 1989 (lee at sq.com)
X#
X
X
X# Delete TAB from IFS...  This is so that tabs typed in the input don't
X# get turned to spaces by the V7 shell...
OLDIFS="$IFS"
IFS='
X '
export IFS
if [ x"$1" = x ]
then	file=/dev/null # or this could be an error
else	file=$1
fi
X
X# next two lines are for echo...  Wire these in on slower boxes...
if [ x"`echo -n hello`" = x"hello" ]
then	N="-n"
X	C=
else	N=
X	C='\c'
fi
X
while read line rest
do	case "$line" in
X	.)	if [ x"$rest" = x"" ]
X		then	exit
X		else	echo "$line" "${rest}" >> $file
X			special=yes
X		fi
X		;;
X	'~'v)	IFS="$OLDIFS" ${VISUAL-vi} "$file"
X		special=yes
X		;;
X	'~'p)	cat "$file"
X		special=yes
X		;;
X	'~'q)	echo $N "Quit [yn] ? $C"
X		read ans
X		if [ x"$ans" = x"y" ]
X		then	exit
X		else	special=yes
X		fi
X		;;
X	'~'!*)	rest="`echo \"$line\"|sed 's/^~!//'` $rest"
X		if [ x"$rest" != x ]
X		then	IFS="$OLDIFS" ${SHELL-/bin/sh} -c "$rest"
X		else	IFS="$OLDIFS" ${SHELL-/bin/sh}
X		fi
X		special=yes
X		;;
X	'~'r)	if [ "x$rest" != x -a -r "$rest" ]
X		then	tee -a "$file" < "$rest"
X		else	echo "Can't read $rest" 1>&2
X		fi
X		special=yes
X		;;
X	'~'w)	rest="`echo \"$line\"|sed 's/^~!//'` $rest"
X		if [ x"$rest" != x"" ]
X		then
X		    if [ -d "$rest" ]
X		    then
X			rest="`echo $rest`/`basename ${file}`"
X		    fi
X		    if [ -s "$rest" ]
X		    then # already exists...
X			/bin/ls -ld "$rest"
X			echo $N "Overwrite/append to ${rest}? [ynoa] $C"
X			read ans
X			case "$ans" in
X			[yYoO0]*) # o for oui or overwrite...
X			    cp "$file" "$rest"
X			    ;;
X			[aA]*)
X			    cat "$file" >> "$rest"
X			    ;;
X			*)
X			    echo "$rest" not changed.
X			    ;;
X			esac
X		    else
X			cp "$file" "$rest"
X		    fi
X		fi
X		special=yes
X		;;
X	'~'e)	IFS="$OLDIFS" ${EDITOR-ed} "$file"
X		special=yes
X		;;
X	'~'\?|'~'h)	cat << FEET
Currently appending to $file
Type a line containing only a dot (".") or an EOF (usually ^D) to exit
X ~e		invoke ${EDITOR-ed} on $file
X ~v		invoke ${VISUAL-vi} on current file
X ~p		print current file on screen
X ~q		quit (leaves text so far)
X
X ~! cmd		run cmd as a Unix command (with ${SHELL-/bin/sh})
X ~r file	read the named file
X ~w file	write a copy of ${file} to the named file
X ~h		prints this summary
X ~?		same as ~h
XFEET
X		special=yes
X		;;
X
X	'~'*)	echo 'Unknown ~ escape $line (~? gives a list)' > /dev/tty ;;
X	*)	echo "$line" "${rest}" >> $file ;;
X	esac
X	if [ x"$special" = x"yes" ]
X	then	echo "[a $file: continue]" 1>&2
X		special=no
X	fi
done
END_OF_FILE
if test 2804 -ne `wc -c <'bin/a'`; then
    echo shar: \"'bin/a'\" unpacked with wrong size!
fi
chmod 755 'bin/a'
# end of 'bin/a'
fi
echo shar: End of shell archive.
exit 0


-- 
Liam R. Quin, Unixsys (UK) Ltd [note: not an employee of "sq" - a visitor!]
lee at sq.com (Whilst visiting Canada from England)
lee at anduk.co.uk (Upon my return to England at Christmas)



More information about the Alt.sources mailing list