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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top