Replacement of variable by their content in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacement of variable by their content in a file
# 1  
Old 11-04-2016
Replacement of variable by their content in a file

Dear all,

I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request.

For example:

Code:
[x004191a@xsnl11p317a tmp]$ echo $SHELL
/bin/bash
[x004191a@xsnl11p317a tmp]$ cat dae2.txt
DELETE FROM                  CDH_STGN_${ENV1}.T1
;
INSERT INTO                  CDH_STGN_${ENV2}.T2
                            (
                              ...
                            )

;
[x004191a@xsnl11p317a tmp]$ export ENV1=DEV1
[x004191a@xsnl11p317a tmp]$ export ENV2=DEV2
[x004191a@xsnl11p317a tmp]$ cat test.sh
srcfile=/app/bin/CDH/src/tmp/dae2.txt

tgtfile=$(
          cat ${srcfile} | while read -r line
          do
            eval echo "$line"
          done
         )

echo "${tgtfile}"
[x004191a@xsnl11p317a tmp]$ ./test.sh
./test.sh: eval: line 11: syntax error near unexpected token `newline'
./test.sh: eval: line 11: `echo ('
./test.sh: eval: line 11: syntax error near unexpected token `)'
./test.sh: eval: line 11: `echo )'
DELETE FROM CDH_STGN_DEV1.T1

INSERT INTO CDH_STGN_DEV2.T2
...
[x004191a@xsnl11p317a tmp]$

The substitution seems OK but some characters in the source file are lost (for instance ";") and produce errors ...

Any help would be greatly appreciated,

Thanks a lot,

Didier.
# 2  
Old 11-05-2016
Code:
$ 
$ cat dae2.txt
DELETE FROM                  CDH_STGN_${ENV1}.T1
;
INSERT INTO                  CDH_STGN_${ENV2}.T2
                            (
                              ...
                            )
;
$ 
$ cat tst.sh
srcfile=dae2.txt
tgtfile=$(
          cat ${srcfile} | while read -r line
          do
            line=$(echo "$line" | sed -e 's/;/\\;/g' -e 's/(/\\(/g' -e 's/)/\\)/g')
            eval echo "$line"
          done
         )
echo "${tgtfile}"
$ 
$ ./tst.sh
DELETE FROM CDH_STGN_DEV1.T1
;
INSERT INTO CDH_STGN_DEV2.T2
(
...
)
;
$ 
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 11-05-2016
You are using eval on the contents of an input which may not only creates a potential security risk, it also does not really make sense here since it interprets its arguments as shell commands and in this case the arguments are the result of [ICODE]echo $line[/ICODE], which read from [ICODE]dae2.txt[/ICODE].

And the content of [ICODE]dae2.txt[/ICODE] clearly does not contain shell commands...



To just get the content of a file into a variable, you could consider this (since you are using bash):
Code:
tgtfile=$(<"${srcfile}")

a more general alternative would be:
Code:
tgtfile=$(cat "${srcfile}")


Last edited by Scrutinizer; 11-05-2016 at 05:28 AM.. Reason: Crossed out wrong remarks, because I did not have my brain switched on
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-05-2016
Thanks a lot Scrutinizer but I am afraid I did not explain clearly my problem: my aim is not only to initialize a variable with the content of a file but to substitute the "shell variables" by their content in this source file (file "dae2.txt" in my example).
# 5  
Old 11-05-2016
Well, I am afraid I really did not read you post well enough..

Try this instead, with the usual security caveats that come with the use of eval, especially in combination with environment variables in which one could hide shell commands:
Code:
eval "echo \"$line\""

So make 100% sure you are in total control of both the input file and the environment variables. Probably better to devise a different mechanism altogether..
---
If you want to preserve leading and trailing space, it is best to use:
Code:
while IFS= while read -r line


Last edited by Scrutinizer; 11-05-2016 at 06:49 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-05-2016
How about
Code:
while read line; do eval tgtfile=\""${tgtfile}"$line$'\n'\"; done < $srcfile

This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-05-2016
Thanks to all of you ... Scrutinizer and RudiC, your suggestions are solving my problem ! Smilie
RudiC, I think your suggestion is quite similar to the Scrutinizer's proposal, right ? ... Maybe, is there an advantage that I can not see ?

Thanks again,

Didier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

2. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

3. Shell Programming and Scripting

Variable resolution in File content

I have a file File1 containing lines like below apple ${FRUIT}-Color orange ${FRUIT}-Color banana ${FRUIT}-Color Now, in my shell I'm reading the file like below while read FRUIT DESC; do echo $FRUIT $DESC; done < File1 which outputs - apple ${FRUIT}-Color orange ${FRUIT}-Color... (3 Replies)
Discussion started by: nexional
3 Replies

4. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

5. HP-UX

XML tag name content replacement

Hi, Need to replace an XML tag name contents, please provide any suggestions. Scenario is : <abc_def>Value_some_content</abc_def> Expected output : <abc:def>Value_some_content</abc:def> We have many tag with different names & contents in a file or a string. Please help on the... (3 Replies)
Discussion started by: periyasamycse
3 Replies

6. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

7. Shell Programming and Scripting

How to put content of file into a variable?

For example, I have a simple text file note: this a note a simple note a very very simple notewhen I use this command, temp=$(cat "note.txt")then I echo temp, the result is in one line. echo $temp note: this a note a simple note a very very simple noteMy variable doesn't have newline. How... (7 Replies)
Discussion started by: 14th
7 Replies

8. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

9. UNIX for Dummies Questions & Answers

How to assign the content of a file to a variable?

Hi all, I have a problem here. I have a file and let we take the content of the file is just '32' (only a numeric value in that file). Now I need to assign this numeric value ( value in that file) to a variable. Is that possible? If so, can you plz advice me on this? Thanks in... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies
Login or Register to Ask a Question