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 |
Not sure I followed. Would
Code:
tmp_cnt=`cat $HOME/GENERATE/tmp2.out` |
Yes, that would work.
This will as well (but is a little more easy on resources): Code:
tmp_cnt=$(<$HOME/GENERATE/tmp2.out) |
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 |
Quote:
(digiot posted this correctly) Or use this, which works the same way: Code:
tmp_cnt=$(cat $HOME/GENERATE/tmp2.out) Code:
tmp_cnt=$(<$HOME/GENERATE/tmp2.out) |
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! |
Here's one way to do that:
Code:
#!/bin/bash Code:
01/11/2005 03/19/2001 279 1968 Code:
01/11/2005 |
That worked. THANK YOU, THANK YOU, THANK YOU!!!! You guys are great!
|
Glad to hear that.
You're welcome. |
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/ |
Or maybe
data file Code:
billym.primadtpdev>cat ~/2 Code:
Code:
billym.primadtpdev>bash ~/1 < ~/2 |
All times are GMT -5. The time now is 02:07 PM. |