Echo variable contents into a text file...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo variable contents into a text file...
# 1  
Old 09-02-2013
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:

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

Code:
#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?

Last edited by Ian Pride; 09-02-2013 at 04:42 PM..
# 2  
Old 09-02-2013
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.
Code:
. . . | awk '{print $2, "/media/Windows1 ntfs-3g default 0 0"}' >> /etc/fstab

This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-02-2013
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Storing file contents to a variable

Hi All, I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it. Input = `cat /path/op.diary` Input = $(<op.diary) I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input'... (12 Replies)
Discussion started by: am24
12 Replies

2. UNIX for Dummies Questions & Answers

HTML: Display contents of file using Variable

<tr><td bgcolor=#D7EBAD><b>Instructions :</b></td> <td>`cat temp.txt`</td></tr> Hi Experts, I have an requirement of displaying file contents in HTML mail , for which am using the above approach, Where as the output is kind of not as expected. If there are 5 lines in the file, in the... (4 Replies)
Discussion started by: scott_cog
4 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

How to echo $variable into txt file?

Hello i tried many times echo $variables into text file with no success for example: echo "#!/bin/sh BBHTAG=RFOCLT_check # What we put in bb-hosts to trigger this test COLUMN=RFOCLT # Name of the column, often same as tag in bb-hosts $BBHOME/bin/bbhostgrep $BBHTAG | while read... (5 Replies)
Discussion started by: mogabr
5 Replies

5. Homework & Coursework Questions

How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to read the contents of each field of a file creating user accounts. The file will be of format : ... (6 Replies)
Discussion started by: dude_me5
6 Replies

6. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

7. UNIX for Dummies Questions & Answers

Is echo $variable >> text.txt working in MacOSX?

Hi New at this, but want to learn more. I'm trying this as an Shell Command in MacOSX; newdate='<TIME>' echo $newdate >> /Users/ttadmin/Desktop/test.txt And it don't work. But if I just use; echo <TIME> >> /Users/ttadmin/Desktop/test.txt (<TIME> is an variable that one program... (6 Replies)
Discussion started by: jackt
6 Replies

8. Shell Programming and Scripting

Help setting variable from file contents with spaces

I suppose the easiest way to explain my problem is to show you some code and then show you what the code outputs: -------------------------------------------------- #!/bin/bash echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt" export Foo=$(cat "/tmp/This Filename Has... (4 Replies)
Discussion started by: nrogers64
4 Replies

9. Shell Programming and Scripting

Storing the contents of a file in a variable

There is a file named file.txt whose contents are: +-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+ | Aborted_clients | 0 | | Aborted_connects | 25683... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

10. Shell Programming and Scripting

echo contents of differing variable names

I'm trying to write a script that reads down a listcontaining various columns, if line1 column 1 != 0 then copy various field to variable field1, then check line1 column2 if > 0 this time copy to variable field2 ... Once all columns checked I want to print the value of each variable if it exists... (2 Replies)
Discussion started by: gefa
2 Replies
Login or Register to Ask a Question