1

I've been working on this two days now and I need some input. I'm creating a universal script to check any drive on any devices I have.The only part that is tripping me up that I have not been able to resolve is the megaraid lines in the text file is being changed when run in a for loop from one line to 3x lines (example below) I have been at this many hours now any suggestions on how to keep it from splitting up the "bus -d disk" format and keep it on one line would be awesome.

(single line on creation and when run standard output in normal cat)

$ sudo smartctl --scan |grep megaraid |awk '{print $1,$2,$3}' > megadrives; cat megadrives 
/dev/bus/0 -d megaraid,0
/dev/bus/0 -d megaraid,1
/dev/bus/0 -d megaraid,2
/dev/bus/0 -d megaraid,3

(when run in a for loop splits each line into 3x new lines)

$ for i in $(cat megadrives); do echo $i; done
/dev/bus/0
-d
megaraid,0
/dev/bus/0
-d
megaraid,1
/dev/bus/0
-d
megaraid,2
/dev/bus/0
-d
1
1

Reading lines is usually done with

while IFS= read -r line; do
    echo "$line"
done <inputfile

BTW

You can replace

: > megadrives; cat megadrives

with

: | tee megadrives
-1

Actually I was searching for the answer wrong, I found it. Any space causes a new line.

IFS=$'\n' # make newlines the only separator for j in $(cat ./file_wget_med) do echo "$j" done

Solve the issue from : https://askubuntu.com/questions/344407/how-to-read-complete-line-in-for-loop-with-spaces

2
  • 1
    Spaces do not cause newlines, echo outputs newlines at the end of its output. You splitting the output of your command on spaces when the shell expands your command substitution, which means you loop over the whitespace-delimited words that cat generates.
    – they
    Nov 21 at 10:38
  • When you use an unquoted command substitution -- like $(somecommand) -- the shell splits the result into words, not lines. It also tries to expand any of the words that look at all like filename wildcards into lists of matching files. You can change the definition of a "word" by changing IFS, but fundamentally this is an abuse of the construct, and can lead to other weird problems. It's much better to use something that's intended to deal in lines, like read. Nov 21 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.