The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing the contents of a file in a variable proactiveaditya Shell Programming and Scripting 6 10-28-2009 12:24 PM
read contents of a file with serveral lines & assign it to a variable satyam.sumit Shell Programming and Scripting 2 09-10-2009 09:26 AM
Remove spaces from first field, and write entire contents into other text file carriehoff Shell Programming and Scripting 3 11-11-2008 02:45 PM
put the contents of this file into a variable with multiple lines Nomaad Shell Programming and Scripting 3 04-23-2008 01:39 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2009
nrogers64 nrogers64 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
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 (permalink)  
Old 11-02-2009
2reperry 2reperry is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 15
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 (permalink)  
Old 11-02-2009
nrogers64 nrogers64 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
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 (permalink)  
Old 11-02-2009
2reperry 2reperry is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 15
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 (permalink)  
Old 11-02-2009
nrogers64 nrogers64 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
Ah, that makes sense. You were a big help! Thanks!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0