How to use decimals in while loop in shell script

In summary, Alice is seeking help with her small shell script and is experiencing difficulties with using decimals in the while loop. Grep provides a modified script that works for Alice's needs, but suggests using perl instead. Another user suggests using python and Alice expresses gratitude for the help.
  • #1
alice06
6
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
  • #2
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.
 
  • #3
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
 
  • #4
You should try perl. It's good for this type of stuff.
 
  • #5
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:)
 
  • #6
Yes, hav to learn Pearl/Python or something soon.
Thank u all for helping me out.

Big Thanx

Regards
Alice
 

Related to How to use decimals in while loop in shell script

1. How do I declare a decimal variable in a shell script?

To declare a decimal variable in a shell script, you can use the "declare" command with the "-r" option. For example: declare -r myDecimal=3.14. This will create a variable called "myDecimal" with the value of 3.14 and make it read-only.

2. How do I perform calculations with decimals in a while loop?

To perform calculations with decimals in a while loop, you can use the "bc" command. This command allows you to use a decimal scale and perform basic calculations. For example: echo "scale=2; 3.14 * 2" | bc will return the result of 6.28. You can then use this command within the while loop to perform your desired calculations.

3. How do I compare decimal values in a while loop?

To compare decimal values in a while loop, you can use the "bc" command with the "-l" option. This option allows you to use a larger scale of decimals. For example: if [ $(echo "3.14 > 2.5" | bc -l) -eq 1 ]; then echo "3.14 is greater than 2.5"; fi This will compare the two decimal values and print a message if the first value is greater than the second.

4. How do I format decimal output in a while loop?

To format decimal output in a while loop, you can use the "printf" command with the "%.nf" format specifier. The "n" represents the number of decimal places you want to display. For example: printf "%.2f\n" 3.14159265 will print the value of 3.14. You can use this command within the while loop to format the output of your calculations.

5. How do I handle decimal inputs in a while loop?

To handle decimal inputs in a while loop, you can use the "read" command with the "-p" option to prompt the user for input. You can then use the "bc" command to check if the input is a valid decimal value or not. For example: while true; do read -p "Enter a decimal value: " input; if [ $(echo "$input" | bc -l) -eq $input ]; then echo "Valid decimal input"; break; else echo "Invalid decimal input, please try again"; fi; done This will prompt the user for input and continue to do so until a valid decimal value is entered.

Similar threads

  • Programming and Computer Science
Replies
33
Views
2K
Replies
16
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
7
Views
4K
Replies
1
Views
5K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top