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.
We will be glad to help, but it's helpful to provide details, like what error(s) you get, and what you've done to try to solve the issue? This seems very much like a homework question, but a few things jump out immediately:
You're missing the #!/bin/bash at the top of your script
When executing the script you posted, you're missing the closing backtick on line 4.
You're not using the IFS correctly in the context you're after.
You're not reading the file with the >...you're outputting TO it. To read, you need the < operator.
There are lots of bash scripting tutorials and examples you can find on Google...that's always a good starting point. Try something like:
Code:
while IFS=: read fn ln dob
do
echo -e "firstname : $fn\n\
lastname : $ln\n\
DOB :\t $dob\n\
done < input.txt
I will only say that you're doing just about everything wrong. You need to stop and do a good read of the BashGuide or another good scripting reference, because you really need get the basic concepts down.
One problem I see, for example (other than what was mentioned above) lies here:
Code:
while IFS=':' read line
The IFS setting tells read to split the line on that character and store it in multiple variables. But you only have one variable here, so what's it supposed to do?
And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
Thanks David and TB0ne, as i said I am new to linux and surely love to going thru the BashGuide. TBOne your response did help me under stand how the IFS will work and split the line, so i should be writting some this like
Code:
while IFS=':' read var1 var2 var3 do
# my logic
done < input.txt
thanks for your responses guys, I was able to acheive what was required, i know need to learn a lot more.....
That's a good attitude to have. Keep at it and you'll get there in time.
I was worried that I might've across as too harsh in my last post. But to be honest, there are an impressive number of errors in the small bit of code you posted. I didn't even mention that the quoting is all wrong.
Now that you've got it working, here's how it should probably look:
Code:
#!/bin/bash
infile=input.txt
while IFS=':' read -r firstname lastname dob; do
echo "First name is: $firstname"
echo "Last name is: $lastname"
echo "Date of birth is: $dob"
echo
done <"$infile"
exit 0
Notice here that it's a good idea to keep code and data as separate as possible. Don't hard-code your filenames, but instead configure them in variables at the top of the script.
Last edited by David the H.; 05-07-2013 at 12:11 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.