Echo variable contents into a text file...

Hi all, I am trying to create a script to read my Windows UUIDs and create mounts in fstab. I know there are different and maybe even better ways to mount Windows partitions at boot time, but I always manually create them in fstab successfully. I do a clean install of Ubuntu often and would like to just run a script to do it for me. I have created a script, but I can't echo a variable contents into fstab. I am semi-new at Linux scripting (been batch/dos scripting for years) so please take it easy on me when you see I've done something wrong lol. Here's my code:

#!/bin/bash
#Reads Windows partitions UUID's from blkid and creates mount in fstab
mkdir /media/Windows1
mkdir /media/Windows2
blkid /dev/sda2 | awk '{print $3}' | sed 's/,0//' > $HOME/sda1.txt
blkid /dev/sda6 | awk '{print $2}' | sed 's/,0//' > $HOME/sda2.txt
part1=$(<$HOME/sda1.txt)
part2=$(<$HOME/sda2.txt)
echo "#Windows" >> /etc/fstab
echo $part1 /media/Windows1 ntfs-3g default 0 0 >> /etc/fstab
echo $part2 /media/Windows2 ntfs-3g default 0 0 >> /etc/fstab
rm -f $HOME/sda1.txt
rm -f $HOME/sda2.txt

But in fstab all that appears is

#Windows
/media/Windows1 ntfs-3g default 0 0
/media/Windows2 ntfs-3g default 0 0

I have checked the text files and the UUIDS have been echoed into the files.
Would someone mind telling me what I am doing wrong please?

Why don't you append your data immediately to /etc/fstab in lieu of writing to a file and then reading that into a variable?
Compose the line in one go and append to /etc/fstab, e.g.

. . . | awk '{print $2, "/media/Windows1 ntfs-3g default 0 0"}' >> /etc/fstab
1 Like

Worked perfectly, thank you very much. Still learning how to script, hard to remember everything you can do. Should've thought to pipe it straight to the file.

Anyway, I would still like to know what was going on there.

Thanks again!