Visit Our UNIX and Linux User Community


Help setting variable from file contents with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help setting variable from file contents with spaces
# 1  
Old 11-02-2009
Question 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 Spaces.txt")

VarName=Bar
export $VarName=$(cat "/tmp/This Filename Has Spaces.txt")

echo $Foo
echo $Bar
--------------------------------------------------

This is the output:

This line has spaces
This

How do I get the "Bar" variable to output the full string?

Thanks so much!
# 2  
Old 11-02-2009
It might help if u were to explain what you were trying to accomplish and I might be able to help further. However, in the examples you outlined try adding the following lines at the beginning of the script :

CFS=$IFS
IFS=$(echo)

and then add the following at the end of the script to set the IFS (Internal Field Separator) back.

IFS=$CFS

Should look like this
-----------------------------
#!/bin/bash
CFS=$IFS
IFS=$(echo)

echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt"

export Foo=$(cat "/tmp/This Filename Has Spaces.txt")

VarName=Bar
export $VarName=$(cat "/tmp/This Filename Has Spaces.txt")

echo $Foo
echo $Bar

IFS=$CFS

Output will look like this :

This line has spaces
This line has spaces
# 3  
Old 11-02-2009
It would be a long story for me to explain what I'm doing, but what you said worked! Thank you!

From your post, I learned that IFS stands for "Internal Field Separator", but what about "CFS"?

Thanks again!
# 4  
Old 11-02-2009
CFS is just a variable I created to hold the current internal field separator (hence CFS - - current field separator) before I set it to $echo. And then it's used to set the IFS back to it's original setting (IFS=$CFS).
# 5  
Old 11-02-2009
Ah, that makes sense. You were a big help! Thanks!

Previous Thread | Next Thread
Test Your Knowledge in Computers #817
Difficulty: Medium
In CSS, E[foo="bar"] matches an E element whose "foo" attribute value is exactly equal to "bar".
True or False?

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

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... (2 Replies)
Discussion started by: Ian Pride
2 Replies

4. Shell Programming and Scripting

Select command with variable options having spaces or null contents

Hi, I'm having an issue trying to produce a hierarchical directory menu that has either any directories listed in a specific directory as options or options with spaces in the option content or null content. So the menu function gets passed a base directory, it then lists any .sh scripts in... (6 Replies)
Discussion started by: andyatit
6 Replies

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Read the contents of a file and store them in a variable

Hi Gurus, I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script: #!/bin/ksh while read line do id=`echo $line | cut -f1 -d |` name=`echo $line | cut -f2 -d |` echo $id ... (11 Replies)
Discussion started by: svajhala
11 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

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

Featured Tech Videos