LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash read files to variable question (https://www.linuxquestions.org/questions/programming-9/bash-read-files-to-variable-question-564000/)

babag 06-23-2007 02:44 PM

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

dive 06-24-2007 08:17 AM

Usually a \ is used to continue to next line.

ghostdog74 06-24-2007 08:30 AM

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

dive 06-24-2007 08:42 AM

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


babag 06-24-2007 01:26 PM

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

babag 06-24-2007 06:39 PM

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


dive 06-24-2007 10:20 PM

For newline do IFS=$'\n' and save some typing. Might affect script output too.

bigearsbilly 06-25-2007 05:04 AM

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


All times are GMT -5. The time now is 08:17 AM.