How to use decimals in while loop in shell script

This is a discussion on How to use decimals in while loop in shell script within the Linux Programming forums, part of the Platform Specific Boards category; Hi all I hav written a very small shell script in bin bash & its not working. The script is ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    How to use decimals in while loop in shell script

    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

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Don't expect the shell to do floating point arithmetic.
    Use awk or perl to get that affect in the shell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 01:27 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21