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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
while IFS=: read fn ln dob
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:
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
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:
while IFS=':' read -r firstname lastname dob; do
echo "First name is: $firstname"
echo "Last name is: $lastname"
echo "Date of birth is: $dob"
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 01:11 PM.