[comp.unix.questions] sh vs. csh format script

maart@cs.vu.nl (Maarten Litmaath) (06/15/89)

mikey@ontek.UUCP (Mike Lee) writes:
\...
\# csh script to call up nroff on a paragraph
\#
\set temp_file = /tmp/foom.$$
\
\# change default indentation to your liking
\set columns = 2
\if ( $1 != "" ) set columns = $1
\
\echo ".hy 0" >! $temp_file
\echo ".pl 1" >> $temp_file
\
\cat | shift_lines -$columns >> $temp_file
\nroff $temp_file | shift_lines $columns
\
\rm $temp_file
\...

1) You shouldn't use csh for scripts:

	- sh starts up faster
	- sh often is more powerful (csh has built-in arithmetic, arrays,
	  modifiers like `:t', comma-separated lists)
	- sh often is easier/clearer

   From the csh manual:

     Quoting conventions are contradictory and confusing.

     The only way to direct  the  standard  output  and  standard
     error separately is by invoking a subshell, as follows:

          tutorial% (command > outfile) >& errorfile

     Although robust enough for general use, adventures into  the
     esoteric  periphery  of  the  C-Shell  may reveal unexpected
     quirks.

2) `cat |' is almost a no-op: it slows things down

Well, here's a Bourne shell script:

	#!/bin/sh

	# change default indentation to your liking
	columns=${1-2}

	temp_file=/tmp/foobar.$$
	umask 077

	exec 3>&1 > $temp_file 4< $temp_file
	/bin/rm $temp_file

	echo ".hy 0"
	echo ".pl 1"
	shift_lines -$columns
	nroff <&4 | shift_lines $columns >&3
-- 
"I HATE arbitrary limits, especially when |Maarten Litmaath @ VU Amsterdam:
   they're small."  (Stephen Savitzky)    |maart@cs.vu.nl, mcvax!botter!maart

eh@astbe.UUCP (H.Dr.Ehling) (07/23/89)

In article <2760@piraat.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:

    >Well, here's a Bourne shell script:
    > .....

That script  truly  is  smart  in  demonstrating  what  can be done with
filedescriptors in sh and it's a bit cruel in it's  commentless  placing
the of  `rm' (I had to learn it the hard way that open files never die).
Anyway, I'm really grateful having seen it. 

But `shift_lines', is on no *IX I happen to  know.   And  although  a  C
Programm could  easily  be  written, let me hint to vi's Shift Operators
"<" and ">". 

"<" shifts  at  most `shiftwidth' (sw) blanks to the left (it transposes
tabs before doing that) and can be used to remove offset  causing  nroff
to break  filling.  But  You will not loose nonwhite text and deliberate
indents greater than `sw' will leave a few blanks, so nroff  will  still
break at that. 

The script below shows, how to use more of nroffs commands, so  You  can
set indentation and linelength:
    (which actually is more of a right margin, as these lines show. They
    have been  formatted  with  `3!!fill  4', as the text originally was
    (and incidently still is) 3 lines. 
As there  usually  has  to  be  reformatting,  the  script uses 'tr', to
squeeze out fill blanks from a previous formatting run.  Otherwise  gaps
will became  always bigger, as nroff respects existing blanks (except at
the end of a line, I believe). 

    A final  remark:  I  never saw it documented, but vi remembers
    the last shel command. So after having said `!}fill 4 66'  You
    just have to say `!}!' for the next paragraph or `!G!' for the
    remainder of the buffer. 

(But I  did'nt  do  that,  as  filled  programms  are  so  hard to
understand!)
-------------------------------------------------------
: # << this substitute (: == `noop') for #\!/bin sh works
#      everywhere, I believe

# usage: fill [indent] [linelength]
# To be used with vi, e.g.:

# se sw=4 # Set in Your EXINIT, just once; then
	  # Iff there is leading WS to be removed:
# <}      # this shifts Paragraph by (at most) sw Blanks

# !}fill 2 66  (def: 0 72)

indent=${1-0}
llength=${2-72}

temp_file=/tmp/foobar.$$
umask 077

exec 3>&1 > $temp_file 4< $temp_file
/bin/rm $temp_file

echo ".hy 0
.ll $llength
.in $indent"

tr -s ' ' ' ' # To squeeze blanks

nroff <&4 >&3
-------------------------------------------------------

Have fun!
-------------------------------------------------------
-- 
Hans-Juergen Ehling     |  From Europe:  eh@astbe.uucp
                        |  From USA:     ...!pyramid!tub!astbe!eh
GEI Software-Technik    |  Voice:        +49 30/8282497