[SOLVED] Reading a multiple file inside loop and store!
Linux - NewbieThis 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.
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.
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 ....
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..
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.
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.
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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.