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
# 8  
Old 11-05-2016
Actually, this might work as well:
Code:
eval tgtfile=\""$(cat $srcfile)"\"

These 3 Users Gave Thanks to RudiC For This Post:
# 9  
Old 11-05-2016
Please be well aware, that using eval you run a risk of executing - potentially malicious - code, e.g. in a "command substitution". Should the srcfile contain a line like $(rm XX), runnig above might perform unwelcome tasks:
Code:
eval tgtfile=\""$(cat $srcfile)"\"
rm: remove regular empty file 'XX'?

Imagine what happened if it were rm /, not aliased to rm -i .

Last edited by RudiC; 11-05-2016 at 12:48 PM..
# 10  
Old 11-06-2016
RudiC,

thanks a lot for that third solution ... I assume that one is better than the previous ones because it is not necessary to use a loop, right ?

I allow myself to ask you a last question (well, two last questions more precisely) regarding your proposal: I am always lost with the "double quote":
  1. why is it necessary to use 2 times "double quote" in that case ?
  2. why is it necessary to protect the outer "double quote" ?

Thanks again to all of your for your time and dedication,

Didier.

---------- Post updated at 09:27 PM ---------- Previous update was at 07:48 PM ----------

RudiC,

just to complete my previous questions in my last post, below an example which is quite difficult to understand for me: using the double-quote two times (with "protection" of the "outer ones") leads the result I want to obtain (this is the syntax you recommend and this is perfect) and deleting the "inner ones" leads to lose the "spaces" and "carriage returns" ...
Too difficult to me ! Smilie

Code:
[x004191a@xsnl11p317a tmp]$ cat dae.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]$ eval echo \""$(cat dae.txt)"\"
DELETE FROM                  CDH_STGN_DEV1.T1
;
INSERT INTO                  CDH_STGN_DEV2.T2
                            (
                              ...
                            )
;
[x004191a@xsnl11p317a tmp]$ eval echo \"$(cat dae.txt)\"
DELETE FROM CDH_STGN_DEV1.T1 ; INSERT INTO CDH_STGN_DEV2.T2 ( ... ) ;
[x004191a@xsnl11p317a tmp]$


Last edited by rbatte1; 11-07-2016 at 06:13 AM.. Reason: Converted to formatted number-list
# 11  
Old 11-07-2016
Well, I hope I can explain the behaviour adequately, but mayhap the other experts in here may jump in and correct/improve my comments...

shells, when expanding a line, and IFS is defined as the default chars (space, tab, newline), replace sequences of IFS chars by a single space unless (part of) the line is quoted. Single quotes preserve the string literally (no further expansion) while double quotes allow for expansions inside the string.
Now, eval runs the line through expansion twice. While the unescaped double quotes preserve the line's newline chars et al. in the first run, the escaped ones do so in the second. Escaping keeps them from being paired with the unescaped annihilating the entire effect on the line (i.e. closing the quotation immediately again).

I hope this was not TOO confusing.
This User Gave Thanks to RudiC For This Post:
# 12  
Old 11-07-2016
Thanks a lot RudiC, Smilie ... that was not too confusing but very clear ! Smilie
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