32 Displaying a File with Additional Information


#32 Displaying a File with Additional Information

Many of the most common Unix commands have evolved within slow-throughput, expensive output environments and therefore offer minimal output and interactivity. An example is cat : When used to view a short file, it really doesn't have much helpful output. It would be nice to have more information about the file. This script, a more sophisticated variation of Script #31, accomplishes this.

The Code

 #!/bin/sh # showfile - Shows the contents of a file, including additional useful info. width=72 for input do   lines="$(wc -l < $input  sed 's/ //g')"   chars="$(wc -c < $input  sed 's/ //g')"   owner="$(ls -ld $input  awk '{print }')"   echo "-----------------------------------------------------------------"   echo "File $input ($lines lines, $chars characters, owned by $owner):"   echo "-----------------------------------------------------------------"   while read line     do       if [ ${#line} -gt $width ] ; then         echo "$line"  fmt  sed -e '1s/^/ /' -e '2,$s/^/+ /'       else         echo " $line"       fi     done < $input   echo "-----------------------------------------------------------------" done  more exit 0 

How It Works

To simultaneously read the input line by line and add head and foot information, this script uses a handy shell trick: Near the end of the script it redirects the input to the while loop with the snippet done < $input . Perhaps the most complex element in this script, however, is the invocation of sed for lines longer than the specified length:

 echo "$line"  fmt  sed -e '1s/^/ /' -e '2,$s/^/+ /' 

Lines greater than the maximum allowable length are wrapped with fmt (or its shell script replacement, Script #14). To visually denote which lines are wrapped continuations and which are retained intact from the original file, the first line of wrapped output has the usual two-space indent, but subsequent wrapped lines are prefixed with a plus sign and a single space instead. Finally, the more program displays the results.

Running the Script

As with the previous script, you can run showfile simply by specifying one or more filenames when the program is invoked.

The Results

 $  showfile ragged.txt  ----------------------------------------------------------------- File ragged.txt (7 lines, 639 characters, owned by taylor): -----------------------------------------------------------------   So she sat on, with closed eyes, and half believed herself in   Wonderland, though she knew she had but to open them again, and   all would change to dull reality--the grass would be only rustling + in the wind, and the pool rippling to the waving of the reeds--the   rattling teacups would change to tinkling sheep-bells, and the   Queen's shrill cries to the voice of the shepherd boy--and the   sneeze   of the baby, the shriek of the Gryphon, and all the other queer + noises, would change (she knew) to the confused clamour of the busy + farm-yard--while the lowing of the cattle in the distance would + take the place of the Mock Turtle's heavy sobs. ----------------------------------------------------------------- 



Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net