tanya@adds.newyork.NCR.COM (Tanya Katz) (03/09/89)
Hi System-V ksh users! I thought it was time I contributed something after receiving lots of useful information and programs from this group! Enclosed is a shell archive of useful scripts: kc - BC front-end, which appeared in UnixWorld last year. CPMV - Interactive move & copy front-end script (similar to rm -i). Its slower but handy if you tend to cp or mv over things. ln CPMV mvi ; ln CPMV cpi In .kshrc: alias cp=cpi & mv=mvi. rmi - Interactively remove files by inode number. Inspired by the fellow who created the file "-i" and because I have done the same type of thing many a time! spellproofer - Interactive spell checker script. (Requires spell). Lets you view the errors in context & correct them as you go. Originally appeared in UnixWorld in 1985. diskcopy - Front-end to cpio & cpiosize. Includes man page. Change to use afio, if you like. Tanya -=-=-= cut -=-=-= cut -=-=-= cut -=-=- cut -=-=-= cut -=-=-= cut -=-=-= cut -= # This is a shell archive. Remove anything before this line! # Save the results in a file. # To unpack "sh file". # To unpack & overwrite existing files, "sh file -c". # PATH=/bin:/usr/bin:/u/bin ; export PATH if test -f CPMV -a "$1" != "-c" then echo file CPMV exists. \"sh $0 -c\" forces overwrite! else echo CPMV 1>&2 cat >CPMV << 'END_OF_CPMV' #---------------------------------------------------------------- #sccs "@(#) CPMV -i Version 1.0" # # Implements the mv -i & cp -i option like BSD. # Link to cpi & mvi. Alias mv=mvi & cp=cpi. # # Author Tanya Katz # tanya@adds.newyork.NCR.COM # ------------------------------------------------------------------------ pgm=`basename $0` usage() { echo "USAGE: $pgm f1 f2 \n\t f1 ... fn d1 \n\t d1 d2" exit 1 } if [ $# -eq 0 ] then usage fi dcnt=0; dpos=0; argc=0 DIR="" if [ "$pgm" = "mvi" ] then CPMV="/bin/mv" else CPMV="/bin/cp" fi for i in $* do argc=`expr $argc + 1` if [ -d "$i" -o "$i" = "." ] then DIR="$i" dpos=$argc dcnt=`expr $dcnt + 1` else entries="$entries $i" fi done case $dcnt in 0) if [ $# -gt 2 ] # Error too many files. then usage elif [ -s "$1" -a -s "$2" ] then echo "$pgm: $2 exists. Overwrite? \c" read resp case $resp in [yY]* ) $CPMV $1 $2 ;; *) ;; esac else $CPMV $1 $2 fi exit $? ;; 1) if [ $dpos -eq $# ] #directory must be last then break elif [ $dpos -eq 1 -a $# -eq 2 ] # or first then $CPMV $DIR $2 exit $? else usage #directory in wrong place fi ;; *) ;; # ignore multiple directories esac if [ -z "$entries" ] # no files??? then usage fi # If we got here, files are being cp/mv to an existing directory. # Any other directories that are located with the source files # are ignored & only the files are being compared... # for i in $entries do name=`basename $i` if [ -d $name ] then continue elif [ ! -s $i ] then echo "$pgm: $i doesn't exist." continue fi if [ -s $DIR/$name ] then echo "$pgm: $DIR/$name exists. Overwrite? \c" read resp case $resp in [yY]* ) $CPMV $i $DIR/$name ;; *) ;; esac else $CPMV $i $DIR/$name fi done exit $? END_OF_CPMV if test 1995 -ne `wc -c <CPMV` then echo sh: "CPMV" unpacked with wrong size! fi fi # ----- End of CPMV shell code ----- # if test -f diskcopy -a "$1" != "-c" then echo file diskcopy exists. \"sh $0 -c\" forces overwrite! else echo diskcopy 1>&2 cat >diskcopy << 'END_OF_diskcopy' # ------------------------------------------------------------------------ #sccs "@(#) Diskcopy Version 1.0" # # CPIO front-end. # # Author Tanya Katz # tanya@adds.newyork.NCR.COM # ------------------------------------------------------------------------ ARG0=`basename $0` if [ $# -eq 0 ] then echo " USAGE: diskcopy -[r|t] diskcopy -[c|d|D] directory dickcopy -f filename" exit 1; fi insflop() { echo "$ARG0: Please insert the first floppy diskette. (q)uit or press <RETURN> \c" read resp if [ "$resp" = "q" ] then exit; else echo " " fi } case $1 in "-c") if [ $# != 2 ] then echo "USAGE: diskcopy -c Path" exit 1; fi echo "$ARG0: cpiosize..." find $2 -depth -print | cpiosize -cBv exit 0 ;; "-r") echo "$ARG0: Ready to copy floppy contents to harddisk..." insflop cpio -icvBmd < /dev/rfp021 ;; "-t") insflop cpio -ictBv < /dev/rfp021 ;; "-d") if [ ! -d $2 ] then echo "USAGE: diskcopy -d Directory" echo "$2: not a directory" exit 1; fi echo "cd $2" cd $2 insflop find . -print | cpio -oBcv > /dev/rfp021 ;; "-D") if [ $# != 2 ] then echo "USAGE: diskcopy -D Directory: absolute path" exit 1; fi if [ ! -d $2 ] then echo "USAGE: diskcopy -D Directory: absolute path" echo "$2: not a directory" exit 1; fi insflop find $2 -print | cpio -oBcv > /dev/rfp021 ;; "-f") if [ $# != 2 ] then echo "USAGE: diskcopy -f filename" exit 1; fi if [ ! -s $2 ] then echo "USAGE: diskcopy -f filename" echo "$2: no such file" fi insflop cat $2 | cpio -oBcv > /dev/rfp021 ;; *) echo " USAGE: diskcopy -[rt] diskcopy -[d|D] directory diskcopy -f filename" exit 1 ;; esac if [ $? != 0 ] then echo "$0: ERROR!! Check the floppy disk & try again.\n" exit 1 else echo "$0: done.\n\n" fi END_OF_diskcopy if test 2083 -ne `wc -c <diskcopy` then echo sh: "diskcopy" unpacked with wrong size! fi fi # ----- End of diskcopy shell code ----- # if test -f diskcopy.1 -a "$1" != "-c" then echo file diskcopy.1 exists. \"sh $0 -c\" forces overwrite! else echo diskcopy.1 1>&2 cat >diskcopy.1 << 'END_OF_diskcopy.1' 'br $Header: diskcopy.1,v 1.20 89/02/05 tfk Exp $ .TH diskcopy 1 local .SH NAME .ad diskcopy \- manipulate archives and files .SH SYNOPSIS .B "diskcopy \-" [ .I t | r ] .br .B "diskcopy \-" [ .I f ] filename .br .B "diskcopy \-" [ .I c | d | D ] directory .SH DESCRIPTION .B diskcopy is a shell script that uses .I cpio to manipulate groups of files, copying them between the filesystem and a floppy .I cpio archive. .SH OPTIONS .TP 12 .BI \-c "\ path" Uses .I cpiosize to estimates the number of floppies necessary to backup the directory. Also gives approximate time of the backup. .TP .B \-d "\ path" Change to the named directory before creating the archive. Creates an archive of paths relative to the current directory. Will traverse and create sub-directories. .TP .B \-D "\ path" Copy the contents of the directory. Does not change to the given directory. The output is not relative, files have absolute pathnames. Will traverse and create sub-directories. .TP .B \-f "\ filename" Create a floppy disk archive using the files designated in the supplied input file. .TP .BI \-r Read the floppy disk archive and copy the contents to the \fBroot drive\fR. .TP .BR \-t Display the table of contents of the floppy archive. .SH CAUTION It is always wise to use the -t option first to check if the archive is absolute or relative, and to make sure you are in the proper directory before extracting files from the floppies. .SH "SEE ALSO" cpio(1), afio(1). .SH AUTHOR Tanya Katz .br .I "..!uunet!ncrlnk!adds" .I "tanya@adds.newyork.ncr.com" END_OF_diskcopy.1 if test 1547 -ne `wc -c <diskcopy.1` then echo sh: "diskcopy.1" unpacked with wrong size! fi fi # ----- End of diskcopy.1 shell code ----- # if test -f kc -a "$1" != "-c" then echo file kc exists. \"sh $0 -c\" forces overwrite! else echo kc 1>&2 cat >kc << 'END_OF_kc' # ------------------------------------------------------------------------ #sccs "@(#) KC Version 1.0" # # BC front-end originally "ksh tool of the month," # UnixWorld Magazine, April, 1988 # # ------------------------------------------------------------------------ if [ $# -gt 0 ] then # exec bc directly with command line arg echo "$*" | bc exit 0 fi # force numbers to upper case typeset -u BCIN M=0 menu() { echo " Kc memory functions: m+\tAdd to memory\t\t\tm- Subtract from memory mi\tMemory in (direct)\t\tmr Memory recall mc\tMemory clear\t\t\t= Print current totals +-/*\tMath op using current total\tac Clear accumulator & memory " } # put bc in background and set up pipe bc -l |& print -p "obase=10; ibase=10; scale=2" print -p "m=0" PROMPT="> " echo "Welcome to KC: the k-shell calculator. Type ? for the help menu. To quit: q <RETURN> " while : do if echo "$PROMPT\c" KBD=`line` then case "$KBD" in help |\?) menu ;; q|Q|quit|QUIT) exit ;; !) ksh ;; !*) eval "${KBD##!}" ;; #strip off leading ! m+) print -p "m = m + ${ANS:=0}; m" read -p M if [ ! "${M}" = "0" ] then print - "\tm= $M" PROMPT="m> " else PROMPT="> " fi ;; m-) print -p - "m = m - ${ANS:=0}; m" read -p M if [ ! "${M}" = "0" ] then print - "\tm= $M" PROMPT="m> " else PROMPT="> " fi ;; mi) print -p - "m = ${ANS:=0}; m" read -p M if [ ! "${M}" = "0" ] then print - "\tm= $M" PROMPT="m> " else PROMPT="> " fi ;; mr) print -p - "m" read -p ANS print - "\t$ANS" ;; mc) print -p "m = 0" PROMPT="> " ;; scale) print -p "scale" read -p ANS print - "\t$ANS" ;; scale*) print -p "$KBD" ;; obase) print -p "obase" read -p ANS print - "\t$ANS" ;; obase*) print -p "$KBD" ;; ibase) print -p "ibase" read -p ANS print - "\t$ANS" ;; ibase*) print -p "$KBD" ;; /0) print "divide by 0\007" ;; / | \* | + | -) ;; +* | -* | /* | \**) BCIN="$KBD" print -p - "${ANS:=0}${BCIN}" if [ $? -ne 0 ] then echo "kc: ${ANS:=0}${BCIN}" exit 1 fi read -p ANS print - "\t$ANS" ;; =) print - "\t${ANS:=0}" if [ $M -ne 0 ] then print - "\tm= $M" fi ;; ac) print -p "m = 0" print -p "0" read -p ANS print - "\t$ANS" PROMPT="> " ;; [!G-Zg-z]*) #matches 0-9 and a-f input if [ ! "$KBD" ] then continue fi BCIN="$KBD" print -p "$BCIN" read -p ANS print - "\t$ANS" ;; esac else exit fi done END_OF_kc if test 2652 -ne `wc -c <kc` then echo sh: "kc" unpacked with wrong size! fi fi # ----- End of kc shell code ----- # if test -f rmi -a "$1" != "-c" then echo file rmi exists. \"sh $0 -c\" forces overwrite! else echo rmi 1>&2 cat >rmi << 'END_OF_rmi' # ----------------------------------------------------------------------- # @(#)rmi Remove files by inode - version 1.0 # # Author: Tanya Katz # Date: 2/22/89 # ----------------------------------------------------------------------- if [ $# -eq 0 ] then echo " USAGE: rmi file(s) " exit 1 fi DOT=`dirname $1` # get the inode number of the file. FILES=`ls -i $*` for INO in $FILES do case $INO in [0-9]*) find $DOT -inum $INO -exec rm -i {} \; ;; *) ;; esac done END_OF_rmi if test 504 -ne `wc -c <rmi` then echo sh: "rmi" unpacked with wrong size! fi fi # ----- End of rmi shell code ----- # if test -f spellproofer -a "$1" != "-c" then echo file spellproofer exists. \"sh $0 -c\" forces overwrite! else echo spellproofer 1>&2 cat >spellproofer << 'END_OF_spellproofer' ###################################################################### # # SPELLPROOFER+ # Originally appeared in UnixWorld Magazine, 9/85 # # Version 1.0 Copyright 1985 by Michael Elola # Version 1.1 modified 10/85 by Philip Restuccia # Tanya Katz: Version 1.2 11/87 ported to UNIX Sys V. # Version 2.0 12/88 Extended interaction & graphics. # # **NOTE: Nextscr has 2 versions for tput capname capability. # Use the one that best suits your system. # ###################################################################### trap "/bin/rm -f /tmp/sp[0-3]$$;exit 1" 1 2 3 13 15 FB=`tput smso` FR=`tput rmso` ARG0="SPELLPROOFER" excused="" term="[^a-zA-Z0-9_]" filelist="$*" args=$# goodspell=1 ERR=/tmp/sp3$$ TMP0=/tmp/sp0$$ TMP1=/tmp/sp1$$ TMP2=/tmp/sp2$$ ######################################################### # $FB S P E L L P R O O F E R + Version 2.0 $FR BANNER () { tput clear echo " S P E L L P R O O F E R + Version 2.0 " } ######################################################### # Nextscr for limited tput capname capability. #nextscr () { BANNER #} # Nextscr for extensive tput capname capability. nextscr () { echo `tput cup 4 1` echo `tput ed` } ######################################################### help1 () { echo " Enter:\n ? <RETURN> = Display $FB $word $FR in context.\n <RETURN> = No change.\n Replace $FB $word $FR with \c" help=1 } ######################################################### help2 () { echo " Enter:\n <RETURN> = No change.\n Replace $FB $word $FR with \c" help=2 } ######################################################### nospello () { nextscr echo " No spelling errors detected in: $filelist\n Good work $LOGNAME!\n " /bin/rm -f $ERR 2> /dev/null exit 0 } ######################################################### # SCRIPT BEGINS HERE... ######################################################### case $args in 0) echo "USAGE: $ARG0 file(s)" exit 1 ;; 1) skipchk=1 ;; *) skipchk=0 ;; esac BANNER echo "working..." wordlist=`spell $filelist 2> $ERR` if [ -z "$wordlist" ] then if [ ! -s $ERR ] then nospello else nextscr; echo "$FB>>> ERROR >>>$FR \c" cat $ERR; echo "\n$ARG0 cannot continue.\n\n" /bin/rm $ERR exit 1 fi elif [ -s $ERR ] then nextscr; echo "$FB>>> ERROR >>>$FR \c" cat $ERR; echo "\n\n\t\tContinue? <y/n> <.>\b\b\c" /bin/rm $ERR read go case $go in [Yy]) ;; *) exit 1 ;; esac fi ### line 127 -- see note below... while echo " Use a local dictionary <RETURN=none> named: \c" do read dictionary case $dictionary in "") break ;; *) if [ ! -r $dictionary ] then echo "$ARG0: can't open $dictionary" dictionary="" continue else break fi ;; esac done for word in $wordlist do # Lookup misspelled word in local dictionary. # case $dictionary in "") ;; *) found=`grep -c $word, $dictionary` case $found in 0) ;; *) continue ;; esac ;; esac goodspell=0 # # Display the unmatched word # nextscr while [ 1 ] do if [ -s $TMP2 ] then cat $TMP2 help2 else echo "\t\t$FB $word $FR" help1 fi read correction if [ -n "$correction" ] then nextscr if [ "$correction" = "?" ] then if [ $help -eq 2 ] then continue fi fi case $skipchk in 1) hitlist=$filelist echo "Searching... \c" ;; 0) hitlist="" echo "Searching files... \c" for file in $filelist do found=`egrep -c "$word" $file` case $found in 0) ;; *) hitlist="$hitlist $file" ;; esac done ;; esac if [ "$correction" = "?" ] then # # Display word in context... # > $TMP2 for file in $hitlist do sed < $file > /dev/null " :start /$term$word$term/ { s/\($term\)$word\($term\)/\1$FB$word$FR\2/gw $TMP2 n bstart } /^$word$term/ { s/^$word\($term\)/$FB$word$FR\1/gw $TMP2 n bstart } /$term$word\$/ { s/\($term\)$word\$/\1$FB$word$FR/gw $TMP2 n bstart } " done if [ ! -s $TMP2 ] then echo " $FB $word $FR not found.\n >> Possibly located in nroff macro file?\n\n\t\tPress <RETURN> <.>\b\b\c" if [ -f $TMP2 ] then /bin/rm $TMP2 fi read pause nextscr break fi nextscr continue fi > $TMP2 for file in $hitlist do nextscr echo "FILE $file:\n" # # Substitute correction for word... # sed < $file > $TMP1 " :start /$term$word$term/ { s/\($term\)$word\($term\)/\1$correction\2/gw $TMP2 n bstart } /^$word$term/ { s/^$word\($term\)/$correction\1/gw $TMP2 n bstart } /$term$word\$/ { s/\($term\)$word\$/\1$correction/gw $TMP2 n bstart } " # # Highlight corrected word... # if [ -s $TMP2 ] then /bin/mv $TMP2 $TMP0 sed < $TMP0 > /dev/null " :start /$correction/ { s/$correction/$FB$correction$FR/gw $TMP2 n bstart } " cat $TMP2 echo " Save corrections in file $file? <y/n> <.>\b\b\c" /bin/rm -f $TMP2 $TMP0 2> /dev/null read response case $response in y) /bin/mv -f $TMP1 $file ;; *) /bin/rm -f $TMP1 ;; esac fi done # Correcting word in all files... break else if [ -f $TMP2 ] then /bin/rm -f $TMP2 fi excused="$excused $word" break fi done done # # Build list of possible dictionary entries. # if [ -n "$excused" ] then nextscr echo $excused | tr ' ' '\012' | pr -5 -t -l1 echo " Do you wish to have some or all of the above words entered into a local dictionary file? <y/n> <.>\b\b\c" read response case $response in y) if [ -n "$dictionary" ] then echo " Append to \"$dictionary\"? <y/n> <.>\b\b\c" read response case $response in y) ;; *) dictionary='' ;; esac fi if [ -z "$dictionary" ] then echo " Enter new/alternate local dictionary file: \c" read dictionary fi echo " Do you wish to be selective? <y/n> <.>\b\b\c" read select echo for word in $excused do case $select in y) nextscr echo "Include $word? <y/n> <.>\b\b\c" read response case $response in y) echo $word, >> $dictionary ;; esac ;; *) echo $word, >> $dictionary ;; esac done ;; esac fi if [ $goodspell -eq 1 ] then nospello else echo "\n\nDone!\n" /bin/rm -f $ERR 2> /dev/null fi exit 0 ###############Insert this on line 127 if spell's errors get put on stdout! #errlist=$wordlist #for err in $errlist #do # if [ "$err" = "spell:cannot" ] # then nextscr; echo "$FB>>> ERROR >>>$FR $errlist\n\n" # echo "$ARG0 cannot continue.\n\n" # /bin/rm $ERR # exit 1 # else # break # fi #done ################## END_OF_spellproofer if test 7079 -ne `wc -c <spellproofer` then echo sh: "spellproofer" unpacked with wrong size! fi fi # ----- End of spellproofer shell code ----- # # End of sh archive. ------------------------------------------------------------------------------ ### ###### ###### ##### US MAIL: Tanya Katz # # # # # # # ADDS, Inc. # # # # # # ##### 100 Marcus Blvd. ####### # # # # # Hauppauge, N.Y. 11788 # # ###### ###### ##### UUCP : ncrlnk!adds!tanya tanya.katz@adds.newyork.NCR.COM Applied Digital Data Systems, Inc. PHONE: (516) 231-5400 X430 ------------------------------------------------------------------------------