Re: OT: bash script - unexpected exit



On Mon, Jan 16, 2012 at 09:25:30PM +0100, Andre Speelmans wrote:
I was not trying, the OP had a script that did a:
cat file | while read line; do something done

Matthew commented that you should not rely on cat for reading a file
line by line and I was curious as to why not. ...

I'm not aware of any particular reason not to use 'cat', and while it
doesn't cost a lot, it's usually less expensive to use redirection.

I just wanted to remind people that another way to redirect input (and
output) is through the exec command. For instance:

exec 0</usr/tmp/foo
exec 1>/usr/tmp/bar
exec 2>/usr/tmp/error

while read INLINE
do
echo $INLINE
done

would read lines from file "/usr/tmp/foo" and output them to
"/usr/tmp/bar", all within the shell. Moreover, any errors (should be none
with something this simple, of course) would go to "/usr/tmp/error" as
STDOUT.

But wait! There's more!

You can redirect to/from any numeric file descriptor. For instance:

exec 3</usr/tmp/foo

while read INLINE <&3
do
echo $INLINE
done

would open file descriptor 3 on "/usr/tmp/foo", and the "while read" will
walk through that file line by line.

You can close descriptors in a script when done with them, e.g.,

exec 3>&-

would close that descriptor opened in the above script snippet.

Cheers,
--
Dave Ihnat
dihnat@xxxxxxxxxx
--
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


Relevant Pages

  • Re: Inline not working properly in GCC?
    ... functions were allowed to have multiple identical definitions, but GCC is ... $ cat f.h ... Any function with internal linkage can be an inline function. ... An inline definition does not provide an external definition for the ...
    (comp.lang.c)
  • Re: Keyboard mapping - keys are wrong.
    ... Benjamin Gawert wrote: ... cat /etc/kbdlang ... There was no way to redirect it to a file and no way to redirect it to more. ... Minefield Consultant and Solitaire Expert. ...
    (comp.sys.hp.hpux)
  • Re: PING ANDREW - slrn and fonts
    ... Troy Piggins is quoted ... & my replies are inline below: ... Oh, and I know this is a stupid cat and pipe combo, shoulda just ...
    (news.software.readers)
  • Re: Cant reach cheapbytes store
    ... Does anyone have another seller of the DVD that I can use or can ... There was a redirect to ...
    (Fedora)
  • Re: redirect to start of file
    ... Since>> apends to the end of a file, how can I redirect to the start ... cat "$FILE" ... Shell Scripting Recipes: | My code in this post, if any, ...
    (comp.os.linux.misc)