Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-19-2012, 12:24 PM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 19
Rep:
|
Reading a multiple file inside loop and store!
Hi ,
This might be a repeated question but i searched alot to figure out a way.. but unfortunately i couldn't find...
i have a file file.list
cat file.list
text1:text2:text3
text4:text5:text6
....
am writing a script to manipulate the strings and store it in a vairable outside the loop
Code:
IFS=':'
while read fa sa ta
do
a=$(echo "$fa" )
b=$(echo "$sa" )
c=$(echo "$ta" )
echo $a
echo $b
echo $c
done < file.list
echo $c
In this case it returns the value of the last line only
but, i want the files to be read line by line and use the inputs outside the loop...
Am sorry if this is already existing query or someone might have answered.. but i couldn't figure out ....
Any help would be really appreciated !!
Thanks...
|
|
|
06-19-2012, 12:42 PM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
Firstly, the echoes are redundant as you can simply assign one variable to another:
You may need to explain further what your requirement is?
Currently the value of variable 'c' will of course only hold the last value assigned to it, which in your example would be 'text6'.
Would you explain further what you think "should" be in 'c'?
|
|
|
06-19-2012, 12:57 PM
|
#3
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
yeah...correct ... it will display text6...
Precisely,
If the script reads the first line it will have 3 variable that is in this case a,b,c and i want those variables to be used outside the loop ....
and subsequently after reading each line it has to perform some tasks with the variable outside the loop....
please guide to use the variables stored and make use of that outside the loop anywhere in the script..
Hope this serves the question !
am not constraint with while we can use any...
|
|
|
06-19-2012, 01:11 PM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
If I understand correctly I would use arrays to access your data:
Code:
while IFS=':' read fa sa ta
do
a+=( "$fa" )
b+=( "$sa" )
c+=( "$ta" )
done < file.list
echo "${c[*]}"
|
|
|
06-19-2012, 01:17 PM
|
#5
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Its giving error...
./small.sh: line 39: syntax error near unexpected token `"$fa"'
./small.sh: line 39: ` a+=( "$fa" )'
am using bash shell and aix 5.3
Last edited by rameshpaul_cog; 06-19-2012 at 01:18 PM.
|
|
|
06-19-2012, 01:19 PM
|
#6
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
Quote:
Originally Posted by rameshpaul_cog
Its giving error...
./small.sh: line 39: syntax error near unexpected token `"$fa"'
./small.sh: line 39: ` a+=( "$fa" )'
am using bash shell and aix 5.3
|
It would help if you'll let us know what shell you are using
--C
|
|
|
06-19-2012, 01:23 PM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
lemme explain my whole requirement.. hope that fills the gap
i have to execute a sql in unix based upon the input values of the file.list
once if i read the first line.. it has to sent the inputs to other file
@$a:$b/$c in this format ...i have to redirect the output to other file say output.txt
like wise it has to manipulate for all the lines of the file.list
If you need more explanation... i will give .. but... am not getting any method to perform this...
Last edited by rameshpaul_cog; 06-19-2012 at 01:30 PM.
|
|
|
06-19-2012, 01:25 PM
|
#8
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Quote:
Originally Posted by custangro
It would help if you'll let us know what shell you are using
--C
|
Am using bash shell
|
|
|
06-19-2012, 01:32 PM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
I would guess it may be an older version of bash (needs to be 3.1 or newer).
Why do you need to store in anything after the loop if all you need to do is simply make a new file?
Code:
while IFS=':' read fa sa ta
do
echo "@$fa:$sa/$ta" >> output.txt
done < file.list
|
|
|
06-19-2012, 01:39 PM
|
#10
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
thanks grail..that was a good one....
but...small prob in that....
i have to echo it and ask for the user input if the details provided are correct...later on only i can redirect it...
and also sorry for confusion...is there any other way that i can use it some where outside the loop instead of redirecting it ?...
that would be really helpful i think.. because....instead of creating a file i can use outside the loop right ?...and perform the same which i want to do in the created file...
let me give my entire script
Code:
IFS=":"
while read a b c
do
echo "-----------------"
echo "$a"
echo "$b"
echo "$c"
done < file.list
while [[ $option != yes && $option != no ]]
do
echo Are the information looks good ? Please answer [yes or no];
read option;
done
if [ "$option" = "yes" ]; then
echo " installing the commands "
@$b/$c [this is the part i want the variables to be used]
elif [ "$option" = "no" ]; then
echo "am not going to install "
fi
Sorry for bothering !!
Last edited by rameshpaul_cog; 06-20-2012 at 04:29 AM.
|
|
|
06-19-2012, 06:30 PM
|
#11
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
The point is that if you use
Code:
while read
.
.
.
done<file
syntax, then it effectively creates a subshell to run the loop in and the vars will not be available outside the loop.
If you have multiple lines on input to process, then out the 'done<file' at the bottom of the script.
|
|
|
06-20-2012, 12:52 AM
|
#12
|
Member
Registered: Feb 2009
Posts: 347
Rep:
|
Does this help?
Code:
#!/bin/bash
i=0
while IFS=":" read fa sa ta
do
x[i]=$fa
((i++))
x[i]=$sa
((i++))
x[i]=$sa
((i++))
done < input1
echo ${x[*]}
|
|
|
06-20-2012, 04:01 AM
|
#13
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
@chrism01 - The example you provided is the one that does not create a sub shell, however the following would:
Code:
cat file | while ...
@OP - Yes you can still use your read to get information from the user within the loop and as demonstrated earlier, simply store the data in arrays is you wish to use
outside the loop.
|
|
|
06-20-2012, 04:25 AM
|
#14
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Thanks Bsat.. ..
Thanks grail....
it works exactly... i can call the variable based upon the i value......thanks lot
Last edited by rameshpaul_cog; 06-20-2012 at 04:30 AM.
|
|
|
All times are GMT -5. The time now is 10:24 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|