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:
query = `cat /Folder/SomeFile.txt`
But when I echo the contents of this file by saying
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.
Thank u for replying. But I need to call the stored procedure by
db2 "call SomeProc(SomeQueryString)"
Now I cannot call it like
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
db2 "call SomeProc('$query')"
but it doesnt work.
Other strange thing is that when I do
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.
No, just call it like this (The var is in quotes so it should be OK):
db2 "call SomeProc($query)"
lol, the extra [ and ] brackets are in your script:
var=[${var}" "${word}]
var="" word="one" newvar="[ one]"
var="[ one]" word="two" newvar="[[ one] two]"
var="[[ one] two]" word="three" newvar="[[[ one] two] three]"
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.
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