Hello there,
I have a script that I would like to read a file line by line and launch a function with the entire line as a parameter. I wrote something that reads the file word after word. Can you help me correct it?
Thanks in advance
Santiago
~$ cat test
myfunction() {
# to make it simple, I just echo the argument
echo "$1"
}
for line in $(cat "$1"); do
myfunction $line
done
~$ cat file
Santiago Diez
Something else
Anything weird
~$ ./test file
Santiago
Diez
Something
else
Anything
weird
Hum, yes.
Actually, I'm pretty newbee in shell script. Altough I've been working for several months with it, I never had any proper lecture about that.
So I just took out what I couldn't understand and noticed that it was still working... Until you mentioned the limits.
So if you have time, could you help me on that:
1) I found no help about read's switch r.
2) How can you write IFS= just before read, even without semi-colon to separate the commands? What's the purpose? Does it modify the value of IFS forever or just in the loop?
Thanks in advance
Santiago
$ help read
read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
...
If the -r option is given, this signifies `raw' input, and
backslash escaping is disabled.
It's set only for the read call. Consider the following:
while IFS= read; do
printf '\nread with empty IFS: |%s|\n' "$REPLY"
read inside<<<"$REPLY"
printf '\nread inside the loop: |%s|\n\n' "$inside"
printf "... so the current IFS is:\n"
od -bc<<<"$IFS"
done<<<" _x_ "
The output is:
read with empty IFS: | _x_ |
read inside the loop: |_x_|
... so the current IFS is:
0000000 040 011 012 012
\t \n \n
0000004
The combination space, tab and newline is the default value.
It's a shell builtin, not an external command.
You will find in it in the bash(or whatever shell supporting that builtin you're using) man pages in the SHELL BUILTIN COMMANDS section.