1

I have a file named param1.txt which contains certain variables. I have another file as source1.txt which contains place holders. I want to replace the place holders with the values of the variables that I get from the parameter file.

I have basically hard coded the script where the variable names in the parameter.txt file is known before hand. I want to know a dynamic solution to the problem where the variable names will not be known beforehand. In other words, is there any way to find out the variable names in a file using the source command in UNIX?

Here is my script and the files.

Script:

#!/bin/bash
source /root/parameters/param1.txt
sed "s/{DB_NAME}/$DB_NAME/gI;
  s/{PLANT_NAME}/$PLANT_NAME/gI" \
  /root/sources/source1.txt >
  /root/parameters/Output.txt`

param1.txt:

PLANT_NAME=abc
DB_NAME=gef

source1.txt:

kdashkdhkasdkj {PLANT_NAME}
jhdbjhasdjdhas kashdkahdk asdkhakdshk
hfkahfkajdfk ljsadjalsdj {PLANT_NAME}
{DB_NAME}
1
  • Welcome to Stack Overflow! Commented Feb 9, 2019 at 17:46

2 Answers 2

0

I cannot comment since I don't have enough points. But is it correct that this is what you're looking for:

How to reference a file for variables using Bash?

Your problem statement isn't very clear to me. Perhaps you can simplify your problem and desired state.

Sign up to request clarification or add additional context in comments.

2 Comments

Not actually.In that particular answer the programmer knew the variable's name beforehand,so he could easily 'echo' it out. What I am interested in knowing is that what if I am not aware of the variable's name, how would I print it's value.
You can't, since you're not aware. How can you know something, if you don't know it.
0

Don't understand why you try to source param1.txt.
You can try with this awk :

awk '
  NR == FNR {
  a[$1] = $2
  next
}
{
  for ( i = 1 ; i <= NF ; i++ ) {
  b = $i
  gsub ( "^{|}$" , "" , b )
  if ( b in a )
    sub ( "{" b "}" , a[b] , $i )
 }
} 1' FS='=' param1.txt FS=" " source1.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.