LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2005, 11:24 AM   #1
llewis
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Rep: Reputation: 0
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
 
Old 01-11-2005, 11:36 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Not sure I followed. Would
Code:
tmp_cnt=`cat $HOME/GENERATE/tmp2.out`
work?
 
Old 01-11-2005, 11:41 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes, that would work.
This will as well (but is a little more easy on resources):
Code:
tmp_cnt=$(<$HOME/GENERATE/tmp2.out)
 
Old 01-11-2005, 11:48 AM   #4
llewis
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 01-11-2005, 12:00 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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)

Last edited by Hko; 01-11-2005 at 12:02 PM.
 
Old 01-11-2005, 12:02 PM   #6
llewis
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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!
 
Old 01-11-2005, 12:20 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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

Last edited by Hko; 01-11-2005 at 12:25 PM.
 
Old 01-11-2005, 12:31 PM   #8
llewis
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
That worked. THANK YOU, THANK YOU, THANK YOU!!!! You guys are great!
 
Old 01-11-2005, 12:34 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Glad to hear that.
You're welcome.
 
Old 01-11-2005, 02:52 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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/
 
Old 01-12-2005, 04:30 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
data missing in variables Dave Kelly Programming 5 11-20-2005 12:51 PM
BASH script – reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
Setting environ. variables before bash starts at login Bralkein Linux - Software 1 03-02-2004 05:39 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration