Need help with Math Scripting


Results 1 to 4 of 4

Thread: Need help with Math Scripting

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Need help with Math Scripting

    Hi all,

    I am trying to figure out how to use a math command to divide one file by another then pipe it to a file.

    I have too files one with a value of 10 (lets say file1) and one with a value of 200 (lets say file 2), I need to divide file 2 by file 1 and had results sent to a file (lets say file 3). I am working out of a book and don't quite understand which operations to use or how they are supposed to be combined.


    Here is what I have so far:

    Code:
     #!/bin/bash
    var3=`echo "var1=file2; var2=file1; var1 / var2" | bc`
    echo output=file3
    but I keep getting the following error.

    ******************************************
    [ Ike]$ ./Week3prog3Ike.scr
    Runtime error (func=(main), adr=15): Divide by zero
    output=file3
    **************************************

    I have tried it this way too

    Code:
    #!/bin/bash
    read File2
    read File1
    var3=`echo "var1=File2; var2=File1; var1 / var2" | bc | output=File3`
    echo
    and I am getting this error output:
    *****************************
    [Ike]$ ./Week3prog3Ike.scr
    quit
    q
    (standard_in) 1: parse error
    (standard_in) 1: parse error
    ******************************

    Thanks in advance.

    COY

  2. #2
    Join Date
    Jul 2003
    Location
    Spokane, Washington
    Posts
    580
    If the numbers are the contents of files, try using the cat command to get the file contents into stdout.

    Remove everything from the ' | bc ...' for now, until you see printed on the screen exactly the text you want being piped into bc.

    I don't think bc can be piped into.

    Bash has some simple arithmetic abilities inside $[[ ]] brackets.

  3. #3
    Join Date
    Mar 2003
    Location
    UK
    Posts
    621
    echo $(echo "$(cat f1)/$(cat f2)"|bc) > f3

    Tried that at the command line and it seemed to work. $(....) is easier to read than backtics. Incidentally I think > is not called a pipe - a 'redirect'?? A pipe is | or a named pipe, a fifo as in mkfifo.
    Last edited by lugoteehalt; 09-19-2011 at 09:52 AM.
    MI6, Offensive Information, Hackers, Encryption, UFO, AOL, Infowar, Bubba, benelux, Ufologico Nazionale, domestic disruption, 15kg, DUVDEVAN, debugging, Bluebird, Ionosphere, Keyhole, NABS, Kilderkin, Artichoke, Badger, spookwords, EuroFed, SP4, Crypto AG – a few, alleged, Echelon keywords. Please add some to your email signature. Full list: http://www.serendipity.li/cia/bz1.html
    http://www.nosoftwarepatents.com/

  4. #4
    Join Date
    Jul 2003
    Location
    Spokane, Washington
    Posts
    580
    Quote Originally Posted by lugoteehalt View Post
    Incidentally I think > is not called a pipe - a 'redirect'?? A pipe is | or a named pipe, a fifo as in mkfifo.
    Yes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •