In my script I need to increment a real variable in a loop, which outputs its value to a file.
I donno how to do this. Can any one help me in this regard?

Thanks

Hey there,

This should work in sh/ksh/bash, etc - incrementing up to 60

number=1
while [ $number -le 60 ]
do
    let number=$number+1 >>outputfile.txt
done

Best Wishes
, Mike

Thanks Mike,

But that is not the problem, probably I haven't framed the question properly.
This is what I want to do

number = 0.1
while [ $number -le 10.0 ]
do
     (echo $number) > input.in ;
     number = $[$number+0.1]
done

Srikanth

Hey there,

This should work in sh/ksh/bash, etc - incrementing up to 60

number=1
while [ $number -le 60 ]
do
    let number=$number+1 >>outputfile.txt
done

Best Wishes, Mike

If you have zsh:

% n=0.1                              
% repeat 10 printf "%.2f\n" $((n+=0.1))
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10

With ksh93:

$ n=0.1
$ for i in {1..10};do printf "%.2f\n" $((n+=0.1));done  
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10

Otherwise, if your shell doesn't support
floating point arithmetic natively:

i=1; n=0.1; while [ 10 -ge "$i" ]; do
	n=$(printf "%f+0.1\n" $n|bc)
	printf "%.2f\n" $n
	i=$(($i + 1))
done

Then just redirect the output to a file.

This one works well. Thank you very much .

Yep

Sorry about that - misunderstood. You do need to trick ksh into doing floating point math as above.

Excellent tips on the other shells, too - Thanks :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.