2

I have two shell script files test1.sh and test2.sh . I have another file called translogs.txt.
Now I need to copy the values of two variables in test1.sh to translog.txt and the same variables need to be copied to the corresponding values in test2.sh.

test1.sh

#!/bin/sh
ONE="000012"
TIME="2013-02-19 15:31:06"
echo -e "$ONE\n$TIME">translog.txt;

translog.txt

ONE="000012"
TIME="2013-02-19 15:31:06"

But here in test2.sh, I want the same value as in translog.txt to the corresponding variable

test2.sh

#!/bin/sh
ONE="000012"
TIME="2013-02-19 15:31:06"
Jens
  • 69,818
  • 15
  • 125
  • 179
Rudra
  • 711
  • 7
  • 13
  • 31
  • I've ready our question 3 times, and still get confused over what exactly do you want to accomplish... Maybe show a before and after along with the way you're currently trying to do it? – Daniel Feb 19 '13 at 11:02
  • I have achieved this test1.sh and translog.txt but I need to get values from translog.txt to test2.sh correspondingly – Rudra Feb 19 '13 at 11:04
  • As I understand it, you want to read from `test2.sh` script values that are put in `translog.txt`? – Blaise Feb 19 '13 at 11:05
  • I need to assign the same values in the translog.txt to same variables in test2.sh – Rudra Feb 19 '13 at 11:06
  • @Blazej Kroll, yes I need to read values to test2.sh – Rudra Feb 19 '13 at 11:06
  • You mean match a ONE="(.+)" and TIME="(.+)" from translog.txt and replace those values for ONE/TIME on test2.sh? – Daniel Feb 19 '13 at 11:07
  • @Daniel, yes I have to get the same values for the variables in test2.sh as in translog.txt – Rudra Feb 19 '13 at 11:09
  • @Vishu: have you managed to make the script work for you? – Blaise Feb 20 '13 at 12:22
  • No...not yet still...working on the same – Rudra Feb 20 '13 at 13:11

4 Answers4

3

1 Diry solution

$> cat translog.txt
ONE="000012"
TIME="2013-02-19 15:31:06"

With perl regular expression grep could match these value using lookbehind operator.

$> grep --only-matching --perl-regex "(?<=ONE\=).*" translog.txt
"000012"

And for TIME:

$> grep --only-matching --perl-regex "(?<=TIME\=).*" translog.txt
"2013-02-19 15:31:06"

So from withing the test2.sh script you can use it like this:

#!/bin/bash
ONE=`grep --only-matching --perl-regex "(?<=ONE\=).*" translog.txt`
TIME=`grep --only-matching --perl-regex "(?<=TIME\=).*" translog.txt`

2 Command line solution

Another solution pointed out in one of the links below would be to use: the source (a.k.a. .) command to load all of the variables in the file into the current shell:

$ source translog.txt

Now you have access to the values of the variables defined inside the file:

$ echo $TIME
"2013-02-19 15:31:06"

3 Easiest solution

Another approach was mentioned by @user2086768. Put these lines to `test2.sh:

#!/bin/bash
eval $(cat translog.txt)

And as a result you would have assigned the two variables within the test2.sh script:

ONE="000012"
TIME="2013-02-19 15:31:06"

you can easily check that adding:

echo $ONE
echo $TIME

Check also these links:

Community
  • 1
  • 1
Blaise
  • 7,230
  • 6
  • 43
  • 53
  • @StevenPenny I don't like to copy&paste another's persons answers. However, I will edit to give more details. – Blaise Feb 19 '13 at 11:26
  • @BlazejKroll I get this error : **grep: ./home/db-backup/translogs.txt: No such file or directory** even when I write the correct code. – Rudra Feb 19 '13 at 11:40
  • @Vishu try now, I removed the unnecessary dots in the scripts. – Blaise Feb 19 '13 at 11:56
  • @Vishu I am assuming that you are running the script from the directory where the translogs.txt is placed. If not, put the correct path to the translog.txt file before it, e.g.: grep --only-matching --perl-regex "(?<=ONE\=).*" \home\vishu\translogs.txt – Blaise Feb 19 '13 at 12:00
  • @BlazejKroll It hangs while executing the script – Rudra Feb 19 '13 at 12:05
  • @BlazejKroll All the three files are in the same directory – Rudra Feb 19 '13 at 12:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24763/discussion-between-vishu-and-blazej-kroll) – Rudra Feb 19 '13 at 12:12
1

As translog.txt is valid bash code, you could do:

source translog.txt

in test2.sh, ONE and TWO would be available in test2.sh.

A word of warning, that does open you up to shell script injection attacks if the values for ONE and TWO were to come from an untrusted source.

beny23
  • 34,390
  • 5
  • 82
  • 85
0

If your translog.txt is as you say, then this will work

#!/bin/sh
while read aa
do
  eval "$aa"
done < translog.txt
Zombo
  • 1
  • 62
  • 391
  • 407
0

eval should work for you.

Try to use this version of test2.sh:

test2.sh

#!/bin/bash
eval $(cat translog.txt)
echo $ONE
echo $TIME

This outputs:

000012

2013-02-19 15:31:06

wooghie
  • 437
  • 2
  • 8