how to read data from a file in shell script

1

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 like ONE and TIME should have the same value as in translog.txt

test2.sh

#!/bin/sh
ONE="000012"
TIME="2013-02-19 15:31:06"

Rudra

Posted 2013-02-19T11:17:15.707

Reputation: 185

Are those the only contents of translog.txt? – Dennis – 2013-02-19T12:27:12.967

@Dennis Yes they are the only contents – Rudra – 2013-02-19T12:31:36.273

Answers

1

If you want the contents of translog.txt to actually look like in your example and you don't mind switching from dash to bash, you can use source (see Shell Builtin Commands in man bash) to simply execute the contents of translog.txt in the current shell.

test1.sh:

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

test2.sh

#!/bin/bash
source translog.txt
echo "ONE:  $ONE"
echo "TIME: $TIME"

Test

$ ./test1.sh
$ cat translog.txt 
ONE="000012"
TIME="2013-02-19 15:31:06"
$ ./test2.sh
ONE:  000012
TIME: 2013-02-19 15:31:06

Dennis

Posted 2013-02-19T11:17:15.707

Reputation: 42 934