How to use decimals in while loop in shell script

  • Thread starter Thread starter alice06
  • Start date Start date
  • Tags Tags
    Loop Shell
AI Thread Summary
The discussion revolves around a shell script issue where the user, Alice, encounters an error due to the use of decimal values in a while loop. The error message indicates that an integer expression is expected, which leads to the conclusion that decimals are problematic in this context. A user named Grep provides a modified script that avoids the decimal issue by using integers and includes a calculation using `bc` for decimal output. Alice successfully adapts Grep's script to meet her needs, expressing gratitude for the assistance. The conversation also suggests exploring other programming languages like Perl and Python for similar tasks, highlighting their effectiveness in handling such calculations. Overall, the thread emphasizes troubleshooting shell scripting issues and the collaborative nature of problem-solving in programming.
alice06
Messages
6
Reaction score
0
Hi all

I hav written a very small shell script in bin bash & its not working. The script is ::

Code:
#!/bin/bash
i=1.0
while [ $i -le 3.0 ]
do
	i=`expr "$i + 0.5" | bc`;
	 echo "i=$i"
done
The error is :
Code:
line 6: [: 1.0: integer expression expected

I think the use of decimals in the while loop is creating problems.

Please help, in urgent need.

Regards
Alice
 
Technology news on Phys.org
I don't write many shells scripts these days, but you're probably right about the decimals in the while loop causing problems. So I just did this:
Code:
#!/bin/bash
i="1"
while [ $i -le 6 ]
do
        i=$[$i+1]
        j=`expr "scale=1;$i.0/2.0" | bc`;
        echo "i=$j"
done
A quickie job, but it seems to work. Had to set the scale in bc to 1 or it truncates the decimals.
 
Hey Grep, Thanx a ton for replying.

Grep, although ur script doesn't give all the numbers -le 6 (or the nuber we specify), but wid a minor modification in ur script, I was able to get my job done perfectly.

Thanx for helping me out, thank u very much.

Regards
Alice
 
You should try perl. It's good for this type of stuff.
 
alice06 said:
Grep, although ur script doesn't give all the numbers -le 6 (or the nuber we specify), but wid a minor modification in ur script, I was able to get my job done perfectly.
No problem at all, alice. Glad to help. Figured it might not be outputting exactly the right numbers, but the basic problem was solved, at least (as I said, quickie job). Glad you got it doing what you wanted.
TylerH said:
You should try python. It's good for this type of stuff.
Fixed that for ya. :wink: o:)
 
Yes, hav to learn Pearl/Python or something soon.
Thank u all for helping me out.

Big Thanx

Regards
Alice
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top