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 | ( \ 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 | ( \ 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 |
Usually a \ is used to continue to next line.
|
you can assign an array while doing the read
Code:
read -a array Code:
# echo ${array[0]} Code:
awk '{print $6 " " $5}' /etc/passwd |
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 |
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 |
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 |
For newline do IFS=$'\n' and save some typing. Might affect script output too.
|
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" Code:
dysp0024:primalA$ ksh ~/1 Code:
dysp0024:primalA$ bash ~/1 job done easy peasy, KIS |
All times are GMT -5. The time now is 08:17 AM. |