Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!killer!ames!mailrus!purdue!gatech!cwjcc!hal!ncoast!allbery From: karl@cs.duke.edu (Karl Ramm) Newsgroups: comp.sources.misc Subject: v04i041: collected sh/csh/awk/etc. goodies (2 of 3) Message-ID: <8808281546.AA09320@romeo.cs.duke.edu> Date: 28 Aug 88 15:46:24 GMT Sender: allbery@ncoast.UUCP Reply-To: karl@cs.duke.edu (Karl Ramm) Lines: 2604 Approved: allbery@ncoast.UUCP Posting-number: Volume 4, Issue 41 Submitted-by: "Karl Ramm" Archive-name: blachman/Part2 From: Nancy Blachman To: net.unix,net.unix-wizards,net.sources Subject: Actual Tricks, shells, csh aliases and the like, 2 of 3 Date: 16 Oct 84 21:22:55 GMT Organization: Resonex Inc., Sunnyvale, CA > [Know anybody with a GREAT .login or .cshrc?] > I'm interested in collecting the little tricks, shell scripts, awk > hacks, csh aliases, and such that people have built to make their daily > life a little easier or more automatic. Being a fairly new system > administrator I don't have the big toolbox that years of messing around > will leave you with. If you have any hacks you're proud of (or that > you aren't proud of, but which work anyway), and you're willing to make > them public, mail them to me. I'll collect, collate, shuffle, sort, > munge, judge, select and discard them and then "summarize to the net". This article focuses shell scripts I received in response to my solicitation. The first article of this series concentrates on aliases, and .cshrc and .login files. The third article centers on C programs and awk scripts. /\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\/ > Nancy Blachman {allegra,hplabs,ihnp4,sun}!resonex!nancy (408)720 8600 x37 < /\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\/ :::::::::::::: shells/1 :::::::::::::: Date: Thu, 13 Sep 84 12:49:57 edt From: allegra!vax135!cornell!tesla!mac (Michael Mc Namara) To: cornell!vax135!houxm!mhuxl!ulysses!allegra!resonex!nancy Subject: Cute cshell - sh scripts If your are like most UNIX systems, you have a plethora of different terminals that your system is supposed to support. Sure, with termcap and terminfo, and programs written to use them, the interface gets easier. If you only ever login to one particular terminal, you can set term = "vt131" or TERM=vt131 export TERM in your .login or .profile. But what if you, or some of your users, drift around using different terminal types-- You have a terminal room with many different terminals. Here is a set of shell scripts & C programs that use the "echo terminal type" command that many terminals support, to do automatic terminal type identification and setting: (of course I've only made it work for the terminals we have here at Cornell, and some day I should write more of it in C...) This one is written for cshell: ########################################## # Identify and set terminal type (csh)# ########################################## set noglob echo Please wait... stty cbreak -echo # this sequence will cause all terminals we have to echo something # except for adm's; those users are told to hit 'a' when they see # please wait echo ' CZ[c' sleep 1 # this calls a c program to grab the id string from the terminal # program is included later... set type = `/usr/new/rin.login` stty -cbreak echo new crt ######################### # the or in the vt100 terminal type is because vt100's respond # to both ^[Z and ^[[c, but DEC threatens to eliminate one of # the responces (I forget which).. The extra stty back you see # with DG terminals (Data General) is because their backspace is # ^Y instead of ^H ( Home on DG terminals) This involves a modification # of stty, which you would care about only if you have them--Write to me. ################## if ( $type == "[?1;0c" || $type == "[?1;0c[?1;0c" ) then set term = vt100 echo "VT101" else if ( $type == "[?1;2c" || $type == "[?1;2c[?1;2c" ) then set term = vt100 echo "VT100" else if ( $type == "[?1;11c[?1;11c") then set term = ct500 echo "[7mCIT - 500[m" else if ( $type == "[?12;5;0;102c" ) then set term = vt100 echo "VT125" else if ($type == "[?1;2c[?1;2c") then set term = vt100 echo "VT100, with Selenar Graphics" else if ( $type == "o#\!R" ) then set term = dg200 stty stop undef start undef back echo "DDG200E" else if ( $type == "o#'C3" ) then set term = dg450 stty back echo "FS11BFS00 DATA GENERAL DASHER D450" else if ( $type == "o#'K3" ) then set term = dg450 stty back echo "FS11BFS00 DATA GENERAL DASHER D450" else if ( $type == "/K" ) then set term = vt52 echo "Visual 50" else if ( $type =~ "\\4*" ) then set term = hp2621 echo HP2621 else if ( $type =~ "a" ) then set term = adm3a echo "adm3a" else if ($type == "50" ) then set term = w50 echo "WYSE50" else # Ask user for terminal type -- for some reason the above didn't work set no_name_yet=1 set no_name_yet = 1; while( $no_name_yet ) echo -n "Please enter terminal type (hit return for menu) " set ttyname = ($< ); switch( $ttyname ) case d450: case dg450: set term=dg450; stty back set no_name_yet = 0; breaksw; case vt100: set term=vt100; set no_name_yet = 0; breaksw; case v50: case vt52: set term=vt52; set no_name_yet = 0; breaksw; case hp2621: case hp: set term=hp2621; set no_name_yet = 0; breaksw; case wyse50: set term=w100; set no_name_yet = 0; breaksw; case adm5: set term=adm5; set no_name_yet = 0; breaksw; case adm3+: case adm3: case adm3a: set term=adm3+; set no_name_yet = 0; case d200: case dg200: set term=dg200; stty stop undef start undef back ; set no_name_yet = 0; breaksw; case other: echo -n "Enter terminal name: " set term=($< ); set no_name_yet = 0; breaksw; case '': echo Terminal abbreviations are: echo "Dasher 450 : dg450" echo "Dasher 200 : dg200" echo "Visual 50 : v50" echo "VT100 : vt100" echo "HP 2621 : hp2621" echo "ADM5 : adm5" echo "ADM3+ : adm3+" echo "Use 'other' to specify terminals not on this menu" breaksw; default: echo "I didn't understand that. Hit return for a menu." endsw end unset no_name_yet unset ttyname endif stty -cbreak stty echo unset noglob Ok, This one is used for Bourne shell lovers: (I used /bin/test because one user had an executable file called test somewhere in his path ahead of where this program lived...That one took days to track down...) echo "Please Wait..." stty start stop crt new back  -echo cbreak echo ' CZ[c' sleep 1 TYPE=`/usr/new/rin.login` stty echo -cbreak if ( /bin/test \( $TYPE = "[?1;0c" \) -o \( $TYPE = "[?1;0c[?1;0c" \) ) then TERM=vt100 echo "[7mVT101[m" elif ( /bin/test \( $TYPE = "[?1;2c" \) -o \( $TYPE = "[?1;2c[?1;2c" \) ) then TERM=vt100 echo "#3[7mVT100[m" echo "#4?[7mVT100[m" elif ( /bin/test $TYPE = "[?1;11c[?1;11c") then TERM=ct500 echo "CT 500" elif ( /bin/test $TYPE = "o#!R" ) then TERM=dg200 stty stop undef stty start undef stty crt stty back echo "D DATA GENERAL DASHER D200E" elif ( /bin/test $TYPE = "o#'C3" ) then TERM=dg450 stty crt stty back echo "FS11BFS00 DATA GENERAL DASHER D450" elif ( /bin/test $TYPE = "/K" ) then TERM=vt52 echo "UVisual 50T" elif ( /bin/test $TYPE = "50" ) then TERM=w50 clear echo "WYSE 50" elif ( /bin/test $TYPE = "\4088000" ) then TERM=hp2621 echo HP2621 elif ( /bin/test $TYPE = "a" ) then TERM=adm3a echo ADM3a else stty start stty stop stty new crt set no_name_yet=1 echo while ($no_name_yet) do { echo echo Which terminal do you have? echo Hit return for a menu. read INP case $INP in 100 | v | vt100) { TERM=vt100 break } ;; adm5) { TERM=adm5 break } ;; adm3+) { TERM=adm3+ break } ;; 450 | dg450 | d450) { TERM=dg450 stty back break } ;; 200 | dg200 | d200) { TERM=dg200 stty stop undef stty start undef stty back break } ;; 50 | vt52 | v50) { TERM=vt52 break } ;; 2621 | hp2621 | hp) { TERM=hp2621 break } ;; other) { echo -n Enter terminal name: read tname TERM=$tname break } ;; ''|?|help) { echo Terminal abbreviations are: echo echo Dasher 450: dg450 echo Dasher 200: dg200 echo Visual 50: vt52 echo VT100 : vt100 echo HP 2621 : hp2621 echo ADM3+ : adm3+ echo ADM5 : adm5 echo "Use 'other' to specify other terminals." echo } ;; * ) { echo "I did not understand that." } ;; esac } done fi export TERM ********* And here is the c program that read's in the terminal ID string: /* rin -- read in a char string which may not be terminated */ char buf[256]; int n; main () { n = read(0,buf,256); write(1,buf,n); printf("\n"); } *************** Use them, abuse them. Of course you have to paw through your terminal manuanls to discover what (if any) string they respond to, and test for the response accordingly... ---MAC :::::::::::::: shells/2 :::::::::::::: From: hplabs!azure!billp To: tektronix!hplabs!resonex!nancy Date: Thursday, 13 Sep 84 09:23:49 PDT Subject: Re: Tricks, shell and awk scripts, csh aliases and the like Here are some that I like and use a lot. 'del' and 'undelete' are just two links to the same file. This way if somebody copies one, they get the other too ------------------------------------------------------------------------------- :::::::::::::: : mv :::::::::::::: : 'prompts if the destination file already exists' /bin/mv -i $* ------------------------------------------------------------------------------- :::::::::::::: : cp :::::::::::::: : 'prompts if the destination file already exists' /bin/cp -i $* ------------------------------------------------------------------------------- :::::::::::::: : del, undelete :::::::::::::: : '"del" throws files into the waste basket' : 'files preceded by "," are automagically removed after 7 days' : '"undelete" pulls files out of the waste basket' : 'if invoked without an argument, both commands list the contents of the' : 'waste basket.' case $0 in *del) if test -z "$1" then ls -lus $HOME/.waste_basket else for i do echo -n "deleting " tmp=`basename $i` /bin/mv -i $i $HOME/.waste_basket/,$tmp echo $i done fi;; *undelete) if test -z "$1" then ls -lus $HOME/.waste_basket else for i do echo -n "recovering " tmp=`basename $i` /bin/mv -i $HOME/.waste_basket/,$tmp $i echo $i done fi;; esac ------------------------------------------------------------------------------- :::::::::::::: : vi :::::::::::::: : 'backs up file(s) before editing them' : 'files preceded by "," are automagically removed after 7 days' for i do if test -f $i then echo -n "backing up " tmp=`basename $i` /bin/cp -i $i $HOME/.vi_backup/,$tmp echo $i fi done /usr/ucb/vi $* ------------------------------------------------------------------------------- Bill Pfeifer {ucbvax,decvax,ihnp4,allegra,uw-beaver,hplabs} !tektronix!tekmdp!billp :::::::::::::: shells/3 :::::::::::::: Date: Thu, 13 Sep 84 13:58:09 pdt From: turtlevax!ken (Ken Turkowski) To: resonex!nancy Subject: Re: Tricks, shell and awk scripts, csh aliases and the like You asked for it, you've got it! Here's ALL of my personal nifties: Ken ************************************************** echo x - bin mkdir bin echo x - bin/CC cat >bin/CC <<'!Funky!Stuff!' # set outname=a.out foreach file ($argv) if ("$file" =~ *.[cso]) then set outname=$file:r break endif end echo cc -o $outname $* exec cc -o $outname $* !Funky!Stuff! echo x - bin/appt cat >bin/appt <<'!Funky!Stuff!' # set notetype=$0 set notetype=$notetype:t set notefile=~/.$notetype # if ($notetype == appt) then # set notetype=appointment # set notefile=~/calendar # endif if ($#argv == 0) then test -t 0 && echo Please enter your $notetype entry '(^D to end):' echo ' ' >> $notefile exec cat >> $notefile endif switch ($1) case -: exec vi $notefile case -rm: cat /dev/null > $notefile exec echo The ${notetype}s are removed. case -what: exec cat $notefile default: echo '' >> $notefile echo "$*" >> $notefile endsw !Funky!Stuff! echo x - bin/asfix cat >bin/asfix <<'!Funky!Stuff!' case $# in 0) file=/tmp/afix$$ trap "cat $file; rm $file; exit" 0 trap "rm $file; exit" 3 9 cat > $file ;; 1) file=$1 ;; *) echo Usage `basename $0` ' [ ]' ;; esac ed - $file << EOF v/[:\.]/s/^/ / g/:./s/:/: / wq EOF !Funky!Stuff! echo x - bin/atcat cat >bin/atcat <<'!Funky!Stuff!' # if ($#argv < 2) then set cmd=$0 echo Usage: "$cmd:t