Thread: How to use decimals in while loop in shell script

  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,059
    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, 12:27 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12: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