I'm trying to put together what I guess could be called a bash script (not that my bash-scripting abilities are anything to brag about). What I'm trying to do is feed a line from a text file--a text file whose content changes on a regular basis--to a program I'm invoking (imagemagick). It's so far working, but only if there is a single word or number sequence in the text file--I think because I'm trying to use a for loop, and the loop somehow results in white space being treated as line breaks. So, when the file's content exceeds one word or numeral sequence, instead of the content from the file being fed to the program as a line of text, it looks like it gets fed one line at a time, and only the last line--pretty much a single word--gets incorporated in the end into the result. Not what I want.

I'll give an example using echo, since I think this will be simpler to demonstrate and understand that way. So I've got a text file, and let's say it's called myfile.txt. It contains a single line with several words and numeric seqeunces. It might look as follows:

'Sep 09, 2016 - 01:00 PM EDT\nconditions: mostly cloudy\n34 F\nHumidity: 39%'

The single quotes are supposed to get the program I'm feeding this material to to treat it as a whole and ignore white space. The \n bits are required by the program I'm feeding it to, and they function in that program as line break indicators. So, using this example with a for loop and the echo command, a line like the following

for i in `cat myfile.txt`; do echo $i; done

produces output not

'Sep 09, 2016 - 01:00 PM EDT\nconditions: mostly cloudy\n34 F\nHumidity: 39%'



From what I'm reading, it seems like using a loop that invokes cat is not a very good way to accomplish what I'm after, and that the loop may well be the cause of the content ending up spread across several lines rather than all together. So, can anyone suggest a way of doing this that will cause all those words and integer groups to end up on the same line?

PS : The script I'm trying to create starts off with #!/bin/bash and consists in a series of commands. It downloads a text file from which most of the content gets deleted and is renamed to myfile.txt. After that it downloads a weather map and performs some operations on it (mainly cropping). The idea is to use imagemagick to juxtapose some text onto that map/image. In fact, it's already working, so long as I do not try to include multiple words or integer sequences divided by white space. If I exceed a single word or integer sequence in the text file, only the last word or integer sequence gets juxtaposed onto the image.

  • Can you post the exact image processing command, where this output is fed as an argument? – chaos Aug 10 '16 at 5:43
  • And, as I understand your question; you don't want to alter the string, you want to pass the string as it is to the image command? So why not just cat myfile.txt. As an argument is would look as follows: command "$(cat myfile.txt)" – chaos Aug 10 '16 at 5:47

I guess you are trying to render some text using Imagemagick. If so, why not say

convert -background lightblue -fill blue -font Helvetica -size 160x  label:"$(<input.txt)" output.gif

where input.txt is the file you want to render.

  • Yes, this appears to be the answer I was looking for. Both this and the "$(cat myfile.txt)" suggestion offered above appear to give the desired results. And I don't need any inverted commas in myfile.txt using this method, either. See, I told you my bash scripting skills were nothing to brag about! Many thanks for the hepful pointers, Michael Vehrs and chaos. – MJiller Aug 10 '16 at 12:31
while read -r things; do
  printf "%s\n" "$things"
done <myfile.txt

If you find yourself writing for i in $( cat ... then stop and change that to a while read -r i instead, and feed in the data at the done. There's no reason to slurp in the entire file's content just to loop over it, and in some contexts (large data) that will land you in a lot of trouble.

The -r flag to read will make read handle backslashes properly.

I'm also using printf rather than echo. Why? See here.

I'm also quoting my variables. Why? See here.

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.