LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Setting Bash Variables from outside data file (https://www.linuxquestions.org/questions/programming-9/setting-bash-variables-from-outside-data-file-276564/)

llewis 01-11-2005 11:24 AM

Setting Bash Variables from outside data file
 
We're new with linux and bash shell scripting. We had an AIX ksh script that use to work and doesn't on linux. We are reading in data from a file on disk and trying to set variables with this data. THe origional script used performed the following:

cat $HOME/GENERATE/tmp2.out | read tmp_cnt

Then we could use the value of the variable tmp_cnt throughout the program. BUt it appears that with linux and bash the read command is only valid for the duration of the read. Then it's lost. I've tried doing the read within a while loop and immediately setting the value to a new variable with strange results. I would appreciate any suggestions. Thanks

slakmagik 01-11-2005 11:36 AM

Not sure I followed. Would
Code:

tmp_cnt=`cat $HOME/GENERATE/tmp2.out`
work?

Hko 01-11-2005 11:41 AM

Yes, that would work.
This will as well (but is a little more easy on resources):
Code:

tmp_cnt=$(<$HOME/GENERATE/tmp2.out)

llewis 01-11-2005 11:48 AM

I just tried your suggestion. This is the code I used:

tmp_cnt='cat $HOME/GENERATE/tmp2.out'
echo "tmp_cnt = $tmp_cnt"



This is the result:

tmp_cnt = cat $HOME/GENERATE/tmp2.out

Hko 01-11-2005 12:00 PM

Quote:

Originally posted by llewis
I just tried your suggestion. This is the code I used:
tmp_cnt='cat $HOME/GENERATE/tmp2.out'
echo "tmp_cnt = $tmp_cnt"

This is the result:

tmp_cnt = cat $HOME/GENERATE/tmp2.out
Use back-quotes ( ` ) instead of single-quotes ( ' )
(digiot posted this correctly)

Or use this, which works the same way:
Code:

tmp_cnt=$(cat $HOME/GENERATE/tmp2.out)
Or, slightly better in my opinion, because it doesn't start a seperate process to run "cat":
Code:

tmp_cnt=$(<$HOME/GENERATE/tmp2.out)

llewis 01-11-2005 12:02 PM

The second suggestion worked! THANKS!!!

I have one more question. Is there a way to read in and set more than one variable from the same file? My origional script performed a SQL select statement that output to a file 4 values. The script then performed the following to read in the values and set them to variables:

cat $HOME/GENERATE/tmp2.out | read curr_date end_date curr_sch_yr_id curr_start_yy


I guess I could break up break up the select output into 4 seperate files and read each in seperately. Something like:

curr_date=$(<$HOME/GENERATE/tmp1.out)
end_date=$(<$HOME/GENERATE/tmp2.out)
curr_sch_yr_id=$(<$HOME/GENERATE/tmp3.out)
curr_start_yy=$(<$HOME/GENERATE/tmp4.out)

Any suggestions would be appreciated. Thanks!

Hko 01-11-2005 12:20 PM

Here's one way to do that:
Code:

#!/bin/bash

args="$*"
set $(<$HOME/GENERATE/tmp2.out)
curr_date=$1
end_date=$2
curr_sch_yr_id=$3
curr_start_yy=$4
test "$args" && set $args

echo "Current date: $curr_date"
echo "End date: $end_date"
echo "Current SCH. year: $curr_sch_yr_id"
echo "Current start year: $curr_start_yy"
echo

This will work if the file looks something like this:
Code:

01/11/2005 03/19/2001 279 1968
or like this:
Code:

01/11/2005
03/19/2001
279
1968


llewis 01-11-2005 12:31 PM

That worked. THANK YOU, THANK YOU, THANK YOU!!!! You guys are great!

Hko 01-11-2005 12:34 PM

Glad to hear that.
You're welcome.

jlliagre 01-11-2005 02:52 PM

Alternatively, you could also have kept you shell script as is, and use either pdksh or the real korn shell, which is freely downloadable (including source code) from its author:
http://www.research.att.com/sw/download/

bigearsbilly 01-12-2005 04:30 AM

Or maybe

data file
Code:

billym.primadtpdev>cat ~/2

XX_abba=hello
XX_beatle=there
XX_cloop="old chap"

script
Code:


billym.primadtpdev>cat ~/1

while read line;do
    eval $line
done
echo $XX_abba $XX_beatle $XX_cloop

result:
Code:

billym.primadtpdev>bash ~/1 < ~/2
hello there old chap



All times are GMT -5. The time now is 02:07 PM.