Problem getting the content of a file in a shell script variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem getting the content of a file in a shell script variable
# 1  
Old 03-29-2011
Problem getting the content of a file in a shell script variable

Hi,
I have a text file that has a long multi-line db2 CTE query. Now I want to store all the contents of this file (i.e. the entire query) in a shell script variable. I am trying to achieve it by this:
Code:
query = `cat /Folder/SomeFile.txt`

But when I echo the contents of this file by saying
Code:
echo $query

then the resultant output is a mess. The output includes the text from the file /Folder/SomeFile.txt and the filenames of other files which are located in the folder where the shell script file is stored, mixed with each other. I want to use this query to pass it to a db2 stored procedure. Please help.
# 2  
Old 03-29-2011
echo "$query"

(you need to quote it)
# 3  
Old 03-30-2011
Thank u for replying. But I need to call the stored procedure by
Code:
db2 "call SomeProc(SomeQueryString)"

Now I cannot call it like
Code:
db2 "call SomeProc("$query")"

So quoting wont help. I understand that quoting the variable helps when we want to echo it but it doesnt help in my case when I'm trying to use it in some other statement. I tried using
Code:
db2 "call SomeProc('$query')"

but it doesnt work.
Other strange thing is that when I do
Code:
 
var=" "
while  read word
  do
      var=[${var}" "${word}]
  done < "Folder/outputFile.txt"
echo $var

Still the output shows all the other file names in the folder where this script is saved. And when I quote it ("$var") then it doesnt show those other filenames in the output but gives these extra [ [ [ [ [ brackets which are not at all there in the file.
And the problem still remains that I cant quote this var in the call to the stored proc.
Plz suggest me what to do.
# 4  
Old 03-30-2011
Quote:
Now I cannot call it like

Code:
db2 "call SomeProc("$query")"
No, just call it like this (The var is in quotes so it should be OK):
Code:
db2 "call SomeProc($query)"

Quote:
but gives these extra [ [ [ [ [ brackets which are not at all there in the file.
lol, the extra [ and ] brackets are in your script:

Code:
var=[${var}" "${word}]

Code:
var="" word="one"  newvar="[ one]"
var="[ one]" word="two" newvar="[[ one] two]"
var="[[ one] two]" word="three" newvar="[[[ one] two] three]"

# 5  
Old 04-01-2011
Thanks for replying. I now get it why I had those extra brackets [[ ( lol ). Even though I can see proper contents of the file on echoing the the variable within quotes. But when I pass this variable to another script, in that other script the variable is expanded as the contents of that file plus some extraaaaa file and subfolder names which are stored along with the first script that has passed this variable. I just cant seem to understand why this is happening.

Say my file contains text:
abc
efg
hijklm

And the script file is stored along with other files like a.txt, b.txt and folders fol1, fol2, fol3 etc.

I call this other script p.sh with the argument -x
ie.
Code:
 
var= "`cat someFile.txt`"
p.sh -x "$var"

But when I see the results poduced by p.sh it contains:
abc
efg a.txt b.txt fol1 fol2
hijklm fol3
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to sort a content of a text file using a shell script?

I am new to shell scripting. I am interested how to know how to sort a content of a file using shell scripting. I've attached the 'Input file' and the 'expected output' to this thread. Details provided in the expected output file will provide details on how the sort needs to be done. ... (16 Replies)
Discussion started by: nkarthik_mnnit
16 Replies

2. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

3. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

4. Shell Programming and Scripting

Execution Problem with dispalying file content using menu driven script

HI All.. below is my menu options script. in option 2,3 and 4 im giving input and they are saving into their respective text file. problem is when im trying to "cat" those files in options 7,8 and 9 im not getting the output. no respective file contents are displaying on screen. but if i... (1 Reply)
Discussion started by: saichand1985
1 Replies

5. Shell Programming and Scripting

How can I write the specific content in the file through shell script

Hello, I need to do one thing that my script creates the file touch release.SPLASH_12_03_00_RC01.txt Now I want to update that file with some content e.g splashbuild::SPLASH_12_17_00_RC02.zip Thanks (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

6. Shell Programming and Scripting

Need to create concatenate the shell variable with file content

Hi Guys, I have a file. Each record needs to inserted into a table. The table also have other columns which needs to be inserted with Shell variables. The following is the file. Error code None. Error Code 1 The shell script is having these variables. Name=Magesh Dep=Coding ... (1 Reply)
Discussion started by: mac4rfree
1 Replies

7. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

8. Shell Programming and Scripting

have a problem in using a variable in kourne shell script

when I write the kourne shell script on a solaris machaine like this: #!/bin/ksh myps =50 echo $myps and when I run this script with typing "myfile" , it shows this message: myfile: myps: not found I think I write a correct syntax but why a program show error like that. (3 Replies)
Discussion started by: thsecmaniac
3 Replies

9. Shell Programming and Scripting

variable lookup problem in shell script

Hi I have one properties file containing as $INSTALL_BASEPATH/mssages/commonmessages_default.properties $INSTALL_BASEPATH/resource/configurationBundle.properties and $INSTALL_BASEPATH is set in .bash_profile but from shell script when I read this file and use in copy statement then it... (7 Replies)
Discussion started by: mnmonu
7 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question