Re: Read/Write files from a script?
The $(<fileName) construct is very nice, if you're using a shell that
supports it. But ash (at least the version I have) doesn't, so what to
do if you want/need to be portable across all Bourne type shells? There
is a feature that will work in all the Bourne shells that I'm familiar
with, using I/O redirection and 'exec':
exec 5<fileName # for reading; to write file do: exec 5>fileName
while read inputLine
And, if you need multiple files open for reading and/or writing, combine
on one line with a single 'exec':
exec 4<existingFile 5>newFile 6>>existingAppend
And so on. The general form to use the file descriptor created in the
exec command line is 'redir-op&#', where # is the desired file
descriptor and redir-op is one of '<' or '>'. Note that when you use
the append form to create the descriptor (as with 6, above) you do *not*
use the append operator when using it. '>&6' will append if 6 was
created in append mode and overwrite if not.
2006. július 19. 16:31,
Mladen Adamovic <firstname.lastname@example.org>
-> Debian User <email@example.com>,:
Tony Terlecki wrote:
Is there a shell command to read files? I want to open a text file,
loop through each line and parse the line of text.
Depending on what you want to do:
Yeah, but he asked about shell commands which read files, and that is 'cat'
so my addon to your answer is : see also 'man cat'
"The purpose of cat is to concatenate (or "catenate") files. If it's only one
file, concatenating it with nothing at all is a waste of time, and costs you
a process." — Randal L. Schwartz
In bash script you might use something like
for i in $(cat yourfile);
In a bash script you should use something like this:
for i in $(<yourfile)