How to use decimals in while loop in shell script

  • Thread starter Thread starter alice06
  • Start date Start date
  • Tags Tags
    Loop Shell
Click For Summary

Discussion Overview

The discussion revolves around the challenges of using decimal numbers in a while loop within a shell script. Participants explore different approaches to handle decimal arithmetic in bash scripting, with a focus on resolving errors encountered when using floating-point numbers.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • Alice describes an issue with her shell script where using decimals leads to an error indicating that an integer expression is expected.
  • Another participant suggests that the problem likely arises from the use of decimals in the while loop and provides an alternative script that uses integers and adjusts the output to include decimals using the `bc` command.
  • Alice acknowledges the help and notes that with minor modifications, the alternative script worked for her needs, although it did not output all the expected numbers.
  • One participant recommends using Perl for handling such tasks, while another suggests Python as a better alternative for similar scripting needs.
  • Alice expresses a desire to learn Perl or Python in the future, thanking the participants for their assistance.

Areas of Agreement / Disagreement

Participants generally agree that handling decimals in bash scripts can be problematic, and multiple approaches are discussed without a clear consensus on the best method. There is no resolution on the effectiveness of the suggested alternatives.

Contextual Notes

Participants mention the need to set the scale in `bc` to avoid truncation of decimals, indicating a limitation in handling floating-point arithmetic directly in bash. There are also references to modifications needed for the alternative scripts to achieve desired outputs.

Who May Find This Useful

Individuals interested in shell scripting, particularly those facing challenges with decimal arithmetic in bash, as well as those considering alternative programming languages for similar tasks.

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, have to learn Pearl/Python or something soon.
Thank u all for helping me out.

Big Thanx

Regards
Alice
 

Similar threads

Replies
33
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K