3

I have been reading lines from a file I created and would like to use a variable and avoid writing to storage. Not sure if this can easily be done. The working code starts off as follows

sensors | grep "Core" > temp.tmp
input=./temp.tmp
while IFS= read -r line
do
--etc--
done < "$input"

The above works fine but I need to find a good location for the temp file and thought I could just avoid writing to storage altogether. Tried the following

input=`sensors | grep "Core"`
while IFS= read -r line
do
--etc--
done < "$input"

This did not work as the newline delimiters were removed and the variable has a huge "line" that is read in all at once. The variable string has ")" that end in the correct place to be used as a delimiter but the "read" keys on newline. Any easy fix?

..thanks for looking...

Joseph Stateson
  • 143
  • 4
  • 14

1 Answers1

5

You don't even need a variable, let alone a file:

sensors | grep "Core" | while IFS= read -r line
do
    command
done

But yes, you could also read from a variable:

input=$(sensors | grep Core)
$ while IFS= read -r line; do echo "$line"; done <<<"$input"
Core 0: +80.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +80.0°C (high = +100.0°C, crit = +100.0°C)
Core 2: +81.0°C (high = +100.0°C, crit = +100.0°C)
Core 3: +80.0°C (high = +100.0°C, crit = +100.0°C)

For more details on the <<< operator and its brethren see:

How to tell which parameter is being supplied to a command with a redirectioin operator?

What are the shell's control and redirection operators?

terdon
  • 104,119