HELP UNIX ppl: Bash scripting

Discussion in 'OT Technology' started by siniquezu, Aug 19, 2004.

  1. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    Can someone help me write a bash script that computates division between 2 decimal numbers?

    Thanks!
     
  2. TMReX

    TMReX Spoooooon!

    Joined:
    May 23, 2003
    Messages:
    50
    Likes Received:
    0
    Location:
    you better check under the sea! or Dallas
    bash in itself doesn't really do math very well. if you need floating point results you are really better off using an external program like bc maybe like this.

    #!/bin/sh

    # floating point precision
    SCALE=2

    RESULT=`echo scale=${SCALE}\; 28472 / 7451 | bc -l`

    echo ${RESULT}
     
  3. Ximian

    Ximian New Member

    Joined:
    Mar 20, 2004
    Messages:
    1,860
    Likes Received:
    0
    Location:
    DCA
    I don't see any reason to do this in bash, you can do it in Perl with a few lines.
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :werd: I'd do it in Tcl myself.
     
  5. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    Sorry for the unix-newbie question but in the code above, how do I adjust the for dfferent values? I know float points are not exactly normal number because they are based on byte arithmetic but how do I script it so that I enter two decimals and it gives me the resulting decimal division? I also understand that bc does computations but it also can't do decimal division which is driving me insane. I have heard in addition you your comments above that it would be better to have an external perl script that does this computation. I am not too saavy on perl so can you guys help me out?
    Thanks!
     
  6. bleak

    bleak Guest

  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I'd do it in Tcl like this:
    Code:
    #!/usr/bin/tclsh
    
    puts stdout [expr 28472 / 7451];
    You could even make it fancy like this:

    Code:
    #!/usr/bin/tclsh
    
    variable num1, num2;
    
    puts stdout "Enter number 1:";
    gets stdin num1;
    puts stdout "Enter number 2:";
    gets stdin num2;
    
    puts stdout [expr $num1/$num2];
     
  8. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,441
    Likes Received:
    12
    expr does not support decimal numbers or answers.
     
  9. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    That's what I thought.
     
  10. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Code:
    [[email protected] /home/adam]$ ./a.tcl
    Enter number 1:
    12.567
    Enter number 2:
    4.364
    2.87969752521
    :dunno:
     
  11. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,224
    Likes Received:
    525
    Code:
    #!/bin/bash
    dc -e "5 k $1 $2 / p"
    the number before the k is your precision value. It divides the first number by the 2nd and returns the answer to <precision> decimal places:

     
  12. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,441
    Likes Received:
    12
    It seems that the expr function in tcl isn't the same as the expr binary a shell would use.
     
  13. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Yeah, Tcl is a fairly advanced scripting language, it handles basic math pretty well.
     

Share This Page