Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11

    Help with math in Red Hat


    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.

    Thanks in advance.

    COY

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    Hey all

    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
    **************************************

    Any help would be greatly appreciated.

    thanks in advance.

    IKE

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    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
    I am getting this error output:
    [Ike]$ ./Week3prog3Ike.scr
    quit
    q
    (standard_in) 1: parse error
    (standard_in) 1: parse error

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    you're close...how about this?
    Code:
    #!/bin/bash
    var1=$(cat file1)
    var2=$(cat file2)
    var3=$(echo $var1 / $var2 |bc -l) # format command and pipe to bc
    echo file1 contains: $var1
    echo file2 contains: $var2
    echo "$var2 / $var1 = $var3"
    My "file1" and "file2" are simple text files, each containing a single line, with a whole number (e.g., 16 in file1 and 4 in file2).

Posting Permissions

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