LinuxQuestions.org
Visit Jeremy's Blog.
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 06-23-2007, 02:44 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 365

Rep: Reputation: 30
bash read files to variable question


found this in reading and intend to try it as a way of sending text
file info to a script as variables:

Code:
cat /etc/passwd | ( \
			IFS=: ; while read lognam pw id gp fname home sh; \
				do echo $home \"$fname\"; done \
				)
the immediate question i have is in regard to formatting the layout
of how this command is written.

the text file in question has many lines, each of which i'd like to
send to its own variable. i already have a version of this script
written for windows powershell. the way that script is layed out i
have each named variable on its own line in the script. in the above
example they are all layed out one after the other in a line.

can i just do something like this?

Code:
cat /etc/passwd | ( \
			IFS=: ; while read 

                                      lognam 
                                      pw 
                                      id 
                                      gp 
                                      fname 
                                      home 
                                      sh; \

				do echo $home \"$fname\"; done \
				)
if there were 50 or 60 more variables it would make reading the
code easier.

do i need to put something in after each variable (like a ; or /) to
tell the script that we're continuing on another line?

thanks,
BabaG
 
Old 06-24-2007, 08:17 AM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
Usually a \ is used to continue to next line.

Last edited by dive; 06-24-2007 at 08:35 AM.
 
Old 06-24-2007, 08:30 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can assign an array while doing the read
Code:
read -a array
then to call the array elements:
Code:
# echo ${array[0]}
# echo ${array[1]}
however, from the example, an easier method is to use awk,
Code:
awk '{print $6 " " $5}' /etc/passwd

Last edited by ghostdog74; 06-24-2007 at 08:57 AM.
 
Old 06-24-2007, 08:42 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
Also you don't need to escape the "" in $fname and the IFS does not have to be part of the read line:
Code:
#!/bin/bash
 IFS=:
 cat /etc/passwd | while read lognam \
                              pw \
                              id \
                              gp \
                              fname \
                              home \
                              sh
                              do
                                 echo $home $fname
                              done
 
Old 06-24-2007, 01:26 PM   #5
babag
Member
 
Registered: Aug 2003
Posts: 365

Original Poster
Rep: Reputation: 30
thanks for the info all.

my source file has spaces in the lines. if i want to change the
IFS from : to a new line, how do i do this? in other words, i
want each line of my source file to be read as a variable. some
of these lines have spaces in them. how do i use IFS to declare
each new line as a seperator without having it get confused by
spaces in the lines?

thanks again,
BabaG
 
Old 06-24-2007, 06:39 PM   #6
babag
Member
 
Registered: Aug 2003
Posts: 365

Original Poster
Rep: Reputation: 30
ok, found the new line thing on IFS but still having problems.

if i echo the first variable, headerA, it prints the entire
file contents to screen.

if i echo anything in addition to the first variable, headerA,
it prints an empty line between each line of the file's
contents.

thanks again,
BabaG

here's what i have:

Code:
#! /bin/bash

IFS="
"

cat $/home/babag/ScriptVariables.txt | while read 	headerA \
						guage \
						stock \
						nullA \
						headerB \
						production \
						camera	\
						capsize \
						capformat \
						outputsize \
						nullB \
						headerC \
						_720cap \
						_1024cap \
						_1280cap \
						_1440cap \
						_1828cap \
						_1920cap \
						_2048cap \
						_2742cap \
						_3072cap \
						_3656cap \
						_4096cap \
						nullC \
						headerD \
						_133 \
						_133Sqz \
						_137 \
						_150 \
						_166 \
						_175 \
						_178 \
						_185 \
						_235 \
						_239 \
						_240 \
						nullD \
						headerE \
						_720 \
						_1024 \
						_1280 \
						_1440 \
						_1828 \
						_1920 \
						_2048 \
						_2742 \
						_3072 \
						_3656 \
						_4096 \
						nullE \
						headerF \
						customW \
						customH \
						outformat \
						nullF \
						headerG \
						receiving \
						odestination \
						pdestination \
						nullG \
						headerH \
						prefix	\
						nullH;

do
echo $headerA 
echo $guage 		
#echo $stock 		
#echo $nullA		

#echo $headerB		
#echo $production	
#echo $camera		
#echo $capsize 	
#echo $capformat	
#echo $outputsize	
#echo $nullB		

#echo $headerC		
#echo $_720cap		
#echo $_1024cap	
#echo $_1280cap	
#echo $_1440cap	
#echo $_1828cap	
#echo $_1920cap	
#echo $_2048cap	
#echo $_2742cap	
#echo $_3072cap	
#echo $_3656cap	
#echo $_4096cap	
#echo $nullC		

#echo $headerD		
#echo $_133 		
#echo $_133Sqz		
#echo $_137		
#echo $_150		
#echo $_166 		
#echo $_175 		
#echo $_178 		
#echo $_185 		
#echo $_235 		
#echo $_239 		
#echo $_240 		
#echo $nullD		

#echo $headerE		
#echo $_720		
#echo $_1024		
#echo $_1280		
#echo $_1440		
#echo $_1828		
#echo $_1920		
#echo $_2048		
#echo $_2742		
#echo $_3072		
#echo $_3656		
#echo $_4096		
#echo $nullE		

#echo $headerF		
#echo $customW		
#echo $customH		
#echo $outformat	
#echo $nullF		
#echo $headerG		
#echo $receiving	
#echo $odestination	
#echo $pdestination	
#echo $nullG		

#echo $headerH		
#echo $prefix		
#echo $nullH		
done
 
Old 06-24-2007, 10:20 PM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
For newline do IFS=$'\n' and save some typing. Might affect script output too.
 
Old 06-25-2007, 05:04 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
do like i do
why not have the variables in the format:

x="y"
name="bert smith"
id=1234323


then you can just source the files in like so:

. var_file

or if you use a decent shell (ksh) you can do this:

Code:
x="nee"
echo $x


echo x="nah" | 
while read line;do
   eval $line
done


echo $x
Code:
dysp0024:primalA$ ksh ~/1
nee
nah
in bash the $x is local to the while loop (rubbish)
Code:
dysp0024:primalA$ bash ~/1
nee
nee

job done easy peasy,
KIS

Last edited by bigearsbilly; 06-25-2007 at 05:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
(Bash) Saving READ values in external .conf files spikylee Linux - Newbie 4 10-28-2003 06:46 AM
bash - read from file question sqn Programming 4 04-01-2003 04:30 AM


All times are GMT -5. The time now is 03:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration