ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Add a second variable in the read statement. The first lexical object will go into the first variable while the rest of the line will go into the second variable.
Code:
...
while read first second
do
let "i=i+1"
echo $i
echo $first $second
done < myfile.txt
You can test it by printing a constant in between $first and $second.
Code:
...
echo $first then $second
...
Last edited by stress_junkie; 02-07-2007 at 10:31 AM.
OK how do I do an if then. Its not working I am not sure whether I need to use the expr statement?
Can somebody show me the correct statement for two number being less than or equal to.
Why don't you just try it and find out? A little self reliance goes a long way.
You could have found out about the read command using multiple variables just by reading the man page. You could find out about using bc in a script by trying it.
You're not going to get very far if you refuse to do the simplest things to help yourself.
Last edited by stress_junkie; 02-07-2007 at 04:43 PM.
for non-integer arithmetic, you can also use zsh which supports it internally, as well as many other nice features.
If you're going to do a lot more sophisticated stuff, I'd consider moving to a more complete language. Perl or Python are both nice, and both have huge libraries of pre-written modules for doing a great deal of stuff. They also both have quite nice mechanisms for you to put your own code in mudules and allow easy re-use/sharing.
C is a lot harder to do well because yuo have to manage your own memory. Of course it has it's moments. There isn't really anything which can touch C for OS coding. (/me dons aspestos underpants).
I am trying the same thing, however I am noting that when I am reading in values from a file, they are not being added as floating point values but instead appear truncated. I have looked but to no avail. Can you suggest where the program is failing.
#!/bin/zsh
let "i = 0"
let "sum = 0"
echo "Min MC: "
read MinMC
#x=2.5
#y=3.456
#(( res = x / y ))
#echo $res
#(( res = x + y ))
#echo $res
#(( res = x * y ))
#echo $res
while read MC Value
do
#echo $MC $Value
if ((MinMC <= MC))
then
((i=i+1))
# echo $i
let "sum=sum+Value"
#else
#echo nothing
fi
#echo ${Value}
done < myfile.txt
echo Sum: $sum
echo count: $i
((average = sum / i))
echo average: $average
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.