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}
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Arnab Basu
  • 11
  • 2

2 Answers2

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.

Kevin C
  • 4,851
  • 8
  • 30
  • 64
  • 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. – Arnab Basu Feb 09 '19 at 15:06
  • You can't, since you're not aware. How can you know something, if you don't know it. – Kevin C Feb 09 '19 at 15:10
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
ctac_
  • 2,413
  • 2
  • 7
  • 17