Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to find longest line, and set as a variable to next command 1

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
I have a script used to produce PDF output from a text file. It needs to be self-contained so it can be used as a filter from an application.

Current script:
Code:
sed '1,2 s/\^L//'|/usr/spool/lp/bin/text2post|ps2pdf -dDEVICEWIDTHPOINTS=792 - | lp -dPC
I need the "792" above to be a configurable variable, based upon the longest line in the print stream.

I tried this:
Code:
sed '1,2 s/\^L//'|tee /usr/local/bin/lcount|/usr/spool/lp/bin/text2post|ps2pdf -dDEVICEWIDTHPOINTS=`cat /usr/tmp/$LOGNAME.lc` - | lp -dPC
The contents of "lcount" are:
Code:
#!/bin/ksh
while read line
do
echo ${#line}
done |sort -n|tail -1 >/usr/tmp/$LOGNAME.lc
As somewhat expected, the result is that my "lcount" gets over-written by the print stream, instead of being used to accept the STDIN and produce the output I want to use later in the script.
Ultimately, this is probably a problematic way to accomplish my goal anyway. I really don't want to resort to creating, then reading a text file just to get the line length. But, I also need to DEVICEWIDTHPOINTS parameter to be reasonable.
There isn't much documentation I can find for ps2pdf, but maybe there is a better way to scale the output to fit a page?


"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
In order for tee to pass data to your script use
...|tee >(/usr/local/bin/lcount)|...
 
Thanks for that PDreyer. This solved the issue of clobbering my "lcount" script with the data stream. My remaining problem is that the resulting temporary text file created by "lcount" isn't available when "ps2pdf" looks for it. Even if I insert a "sleep" command within the "text2post" or "ps2pdf" scripts. It seems that my shell looks to execute that `cat /usr/tmp/$LOGNAME.lc` command first.

Does anybody know of a way to tee the data stream, then wait before the remainder of the piped commands are executed? I'm sure I'm making this harder than it need be.

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Just to close things out, this was my final script:
Code:
#!/bin/ksh
dd of=/usr/tmp/$LOGNAME.$$ 2>/dev/null
longest=`while read line
do
echo ${#line}
done </usr/tmp/$LOGNAME.$$ |sort -n|tail -1`
if [ $longest -gt 80 ]
        then
        WIDTH=792
        else
        WIDTH=576
fi
export WIDTH
 sed '1,2 s/\^L//' < /usr/tmp/$LOGNAME.$$ |/usr/spool/lp/bin/text2post|ps2pdf -dDEVICEWIDTHPOINTS=$WIDTH - | lp -c -dPC -s
rm /usr/tmp/$LOGNAME.$$
I had another variable called "$HEIGHT", but my version of ps2pdf didn't allow me to include both command line options, so I settled for the adjustable WIDTH.

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top