Re: Help needed piping output to a bash script
>
> Can use a hint or two with a 'bash' script...
>
> I've just downloaded and installed the basic 5 or 6 meg Hamm distribution of
> Debian. Couldn't get it to see the modem, (due to a non-Hamm problem, it turned
> out the ISA PNP modem had switched configs, when I'd recently flashed the
> BIOS), and was trying to make do with what was available, sort of as a
> challenge, or puzzle.
>
> I tried to read the available help files, but there was no 'less' in this
> distrib, and most of the time 'more' didn't work right. Having played
> with muLinux, (a small 2-disk Italian distrib with X window, that stays small
> by using odd little scripts), I figured this was a good time to write a 'less'
> script, or just a crude reader.
Best solution is to download the less debian package from the text
section, and install that with `dpkg --install less<version>.deb.
> I figured I'd use 'vi', as that already did paging. So I wrote a script to
> echo a header to a tempfile, (which said something like "Press ':q' to quit",
> in case I forgot how to use 'vi'), append the file named in $1 to this
> tempfile, and run 'vi' to view it. Finally, after leaving 'vi', it deletes the
> tempfile. The tempfile would insure 'vi' wouldn't change the original. It
> worked.
>
> Then I wanted to be able pipe stuff to this viewer. This turned out to
> be The Puzzle. Here's a simplified code snippet that doesn't do what I want:
>
> <snip>
> 1: while read line # read lines from standard input
> 2: do
> 3: echo $line >> $$.mycat.tmp # Append them to a temp file.
> 4: done
> 5: vi $$.mycat.tmp # view the temp file with 'vi'.
> <snip>
>
> If this snippet were called 'mycat', and run like this:
> "ls -a | mycat"
> I expected it to show a directory listing in 'vi'. Doesn't. What apparently
> happens is that standard input is piped to 'vi', which then exits by itself.
> This is a guess, based on the same thing happening when I type: "ls -a | vi"
>
> After the snippet is done, $$.mycat.tmp does contain the desired piped
> directory.
>
> I supposed there'd be no standard input left by line #5. That is, the
> 'read' loop would use it up. 'vi' seems to think differently. What I'm trying
> to learn is not so much how to pipe input directly to 'vi', (though that would
> be OK too), but rather how to make 'read' use up all of standard input, as so
> NOT to pipe input to 'vi'. (Assuming this is indeed what's happening.)
Thought about this for a while. The problem is that the script gets its
standard input from the pipe, and vi needs input from a tty. Your
current tty is /dev/tty. This works:
------------------------------------------------------------------------
#! /bin/bash
# mymore
$TMPFILE=/tmp/mymore.$$.tmp
while read line; do
echo $line >> $TMPFILE
done
vi $TMPFILE < /dev/tty
rm -f $TMPFILE
------------------------------------------------------------------------
HTH,
Eric Meijer
--
E.L. Meijer (tgakem@chem.tue.nl) | tel. office +31 40 2472189
Eindhoven Univ. of Technology | tel. lab. +31 40 2475032
Lab. for Catalysis and Inorg. Chem. (TAK) | tel. fax +31 40 2455054
Reply to: