sh vs. csh format script (was: Re: Vi)

Maarten Litmaath maart at cs.vu.nl
Thu Jun 15 07:15:58 AEST 1989


mikey at 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 at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list