[Solved] bash scripting guru's - at your leisure

General support questions
Post Reply
lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

[Solved] bash scripting guru's - at your leisure

Post by lightman47 » 2021/09/12 18:17:00

I have a bash script I am writing that takes a text shopping list file and builds a similar html file on my apache server that I may call up on my iphone while shopping. (Works fine).

I have a 'for loop' in the script that inserts the lines from the '.txt' input file into the destination .html file. It also works (pretty much) great. The problem is that a <space> within a line in the input file is interpreted as a <carriage-return> and breaks the item into two items. I can VERY easily alter this 'mis-behavior' by substituting <underscore> characters for <space>s in my list and it isn't at all difficult.

Example:
pork roast

becomes
pork
roast

Question:
What basic stupidity am I committing?

I've done "man cat" an not found anything obvious to me.

Code: Select all

##############################################################################

## now add text file entries

for entry in $(cat "$inputFile")
	do
		echo "$entry<br>" >> "$outputFile";
	done

## ??? spaces in lines are interpreted as carriage returns ???
## I have to replace spaces with inderscores? Why is that, I wonder ??

##############################################################################
Thank you.
Last edited by lightman47 on 2021/09/13 10:50:31, edited 1 time in total.

MartinR
Posts: 714
Joined: 2015/05/11 07:53:27
Location: UK

Re: bash scripting guru's - at your leisure

Post by MartinR » 2021/09/12 18:58:31

Really cheap and cheerful:
File X:

Code: Select all

cat XX | while read entry
	do
		echo "$entry<br>" >> XXX
	done
File XX:

Code: Select all

pork roast
biscuits
prawn crisps
chips
Output:

Code: Select all

pork roast<br>
biscuits<br>
prawn crisps<br>
chips<br>

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: bash scripting guru's - at your leisure

Post by jlehtone » 2021/09/12 20:29:08

While I like the cat:

Code: Select all

while read entry
	do
		echo "$entry<br>" >> XXX
	done < XX
https://www.tecmint.com/different-ways- ... sh-script/
https://porkmail.org/era/unix/award.html

MartinR
Posts: 714
Joined: 2015/05/11 07:53:27
Location: UK

Re: bash scripting guru's - at your leisure

Post by MartinR » 2021/09/12 21:34:32

There are many ways to skin a cat! :D

Yep, cheep & cheerful was about 30 seconds thought and maybe two minutes to write and test. I never claimed it as perfect, only that it worked!

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: bash scripting guru's - at your leisure

Post by pjsr2 » 2021/09/13 10:11:42

## ??? spaces in lines are interpreted as carriage returns ???
## I have to replace spaces with inderscores? Why is that, I wonder ??
The shell uses the characters in the environment variable IFS as the field separators.
The default value of $IFS are the three characters space, tab and newline, as yo can see from:

Code: Select all

$ echo -n "$IFS" | od -t a
0000000  sp  ht  nl
0000003

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: bash scripting guru's - at your leisure

Post by lightman47 » 2021/09/13 10:49:59

Thank you!! I commented out my cat line and inserted MartinR's to test - works perfectly.

Code: Select all

##############################################################################

## now add text file entries

cat "$inputFile" | while read entry

#for entry in $(cat "$inputFile")
	do
		echo "$entry<br>" >> "$outputFile"
	done

## ??? spaces in lines are interpreted as carriage returns ???
## I have to replace spaces with inderscores? Why is that, I wonder ??

##############################################################################

tunk
Posts: 1205
Joined: 2017/02/22 15:08:17

Re: [Solved] bash scripting guru's - at your leisure

Post by tunk » 2021/09/13 11:26:59

Or this:
sed -z 's/\n/<BR>\n/g' input.txt > output.txt
A bit of web-searching also suggests that tr could be used.

Post Reply