Problem with dividing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with dividing
# 1  
Old 11-30-2009
Problem with dividing

Hi All,

I have two variables like below..

Code:
ETIMEHR =03            #END TIME HOUR
ETIMEMIN=02            #END TIME MIN

Now I'm converting the min to hour by deviding it by 60
Code:
ETIMEMINCONV=`echo "scale=2; $ETIMEMIN/60" | bc`

but when i print ETIMEMINCONV instead of getting converted value i'm getting an error like "syntax error on line 1"

I'm using this inside for loop.. so for some values it is working and for others it is giving the above said error...

is there any other alternative for this?

thanks&regards,
Deepak.
# 2  
Old 11-30-2009
Can you show more (like "the loop")? For which values is it not working? Can you run the script with -x (set -x) to start debugging the problem?

(I trust that the space before the = in "ETIMEHR =03" is not really there in your actual script)
# 3  
Old 11-30-2009
But no need of ETIMEHR here know.

i didnt get the debug part which you told me above since i'm not a unix programmer.

and yeah in my script set -x is there but how i can find out or debug where i'm getting the error?
# 4  
Old 11-30-2009
I did a test...

Code:
$ cat Test
set -x

X=1

for I in 1 2 3 4 5 6 7 8; do
  X=$(echo "scale=2; $X/$I" | bc)
  echo $X $I
done

$ ./Test
+ X=1
+ + bc
+ echo scale=2; 1/1
X=1.00
+ echo 1.00 1
1.00 1
+ + echo scale=2; 1.00/2
+ bc
X=.50
+ echo .50 2
.50 2
...

If bc gives an error you should see it among this output. Just before that you should see the value of ETIMEMIN (i.e. "echo scale=2 .....") that was used in the "echo ... | bc" command.

If you are able to show the code for the loop (and anything else needed within it) then it would made it easier.
# 5  
Old 11-30-2009
Smilie i dont have the exact code with me now.. It is there in my office system.

i will do as per your suggestion tomorrow and let you know.. Thanks a lot for your advice.

---------- Post updated at 05:36 PM ---------- Previous update was at 05:29 AM ----------

hi scottn

Code:
ENDDAY=11/25/09
+ + echo 11/25/09
ENDMM=11/25/09
+ + awk -F/ {print $2}
+ echo 11/25/09
ENDDD=25
+ + awk -F/ {print $3}
+ cut -c1-2
+ echo 11/25/09
ENDYY=09
+ + grep -i $PATH
+ awk -F; {print $21}
+ awk -F  {print $2}
+ awk -F: {print $1}
+ sort -u
ETIMEHR=06
+ + awk -F; {print $21}
+ awk -F: {print $2}
+ grep -i $PATH
+ sort -u
+ awk -F  {print $2}
ETIMEMIN=02
+ + bc
/60cho scale=2;02
ETIMEMINCONV=syntax error on line 1, 
+ print  syntax error on line 1,  
 syntax error on line 1,

this is what the error i'm getting.. see there it is taking /60 at the beginning instead of 02/60... pls help me..

---------- Post updated at 07:20 PM ---------- Previous update was at 05:36 PM ----------

For some values i'm getting the proper ans as shown below..

Code:
 + grep -i $PATH
+ awk -F; {print $21}
+ cut -d  -f1
+ sed -e s/\$/ /g
ENDDAY=11/30/09
+ + echo 11/30/09
ENDMM=11/30/09
+ + awk -F/ {print $2}
+ echo 11/30/09
ENDDD=30
+ + awk -F/ {print $3}
+ echo 11/30/09
+ cut -c1-2
ENDYY=09
+ + awk -F; {print $21}
+ awk -F: {print $1}
+ grep -i $PATH
+ sort -u
+ awk -F  {print $2}
ETIMEHR=09
+ + grep $PATH
+ awk -F; {print $21}
+ awk -F  {print $2}
+ awk -F: {print $2}
+ sort -u
ETIMEMIN=51
+ X=51
+ + bc
+ echo scale=2; (51/60)
ETIMEMINCONV=.85
+ print  .85
 .85

so please tell me what is wrong with this...

Last edited by smarty86; 11-30-2009 at 11:17 PM..
# 6  
Old 12-01-2009
Perhaps your version of bc has problem with the leading zero. What happens if you use:
Code:
ETIMEMINCONV=`echo "scale=2; ${ETIMEMIN##0}/60" | bc`

# 7  
Old 12-01-2009
hey i dont know what was wrong.. i just multiplied first by 1000 to the value of ETIMEMIN then instead of deviding by 60 i used 60000..so it is working fine now Smilie

Thanks for all your reply...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dividing a column by it's last number

Hi! I have a file as below: 0.000145 5.82056e-08 0.000146 6.82088e-08 0.000150 7.42115e-08 0.000156 8.52142e-08 0.000176 8.82169e-08 0.000178 9.82197e-08 I'd like to divide the values of the second column by its last entry (here 9.82197e-08). I would be thankful if someone could... (6 Replies)
Discussion started by: Setareh12
6 Replies

2. Shell Programming and Scripting

Dividing by zero

Does anyone know how to include as a script maybe an "echo" warning that explains that if a user uses the second number "zero" when dividing, that the result will BE "zero." I need, example: 5/0 (second number) = 0, in script form. current script: echo "Enter a number" read num1 echo... (4 Replies)
Discussion started by: jefferj54
4 Replies

3. Shell Programming and Scripting

Dividing one file into several files

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows, the first column represents the name of populations, in my real file, I have 47 populations and 2172 lines in total with about 47000 fields. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0 I want... (3 Replies)
Discussion started by: Homa
3 Replies

4. Shell Programming and Scripting

Dividing two numbers in unix

Hi, I have written this code the division of two numbers and multiply it by hundred (to get the percent) echo "script running for the 1st time" cp value_directory_present.txt /home/mysql_cnbc/value_directory_past.txt rm -f test6.txt value_directory_present.txt else y1=`awk '{print $1}'... (5 Replies)
Discussion started by: arijitsaha
5 Replies

5. Shell Programming and Scripting

Issues when dividing

Hi, I do have a very simple task to divide 2 variables and display the result. I CANNOT use bc when i try var1=2 var2=4 var3=$(($var1 / $var2)) echo $var3 the output is always 0 What can I change to get a dotted decimal result such as 0.5 ? Thanks! (5 Replies)
Discussion started by: svetoslav_sj
5 Replies

6. Shell Programming and Scripting

Dividing file into columns

Hi, I have a file containing 28048 lines and I would like to split it into a a file with 3506 lines and 8 columns? In column 1, I would like to have the first 3506 lines, in columns 2 the second 3506 lines and so on. I've been looking around in the forum but I found very specific cases. Is... (1 Reply)
Discussion started by: f_o_555
1 Replies

7. UNIX for Dummies Questions & Answers

Dividing a column by it's first number

Hi! Is there an easy way (maybe using awk??) to divide the values of one column of the file by it's first entry.. If I have a column: 3 4 5 6 7 I would like to divide it by 3. I want to do this for more than 100 files, so it wouldn't be practical to open file by file and... (26 Replies)
Discussion started by: cosmologist
26 Replies

8. UNIX for Dummies Questions & Answers

Dividing float values

Hi I know its a dumb question but can any one please explain me the difference of executing a shell script in the following 2 ways. . script.sh sh script.sh I have a problem if I execute the following code as sh script.sh DB_CNT_ALW=0.20 SCT_VAR=0.05 if ; then echo "== Difference... (3 Replies)
Discussion started by: shash
3 Replies

9. Programming

dividing a file into two

Hi .. I have a file which contains a list of numbers like.. 1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027, 2028,2029,2030,2031,2032,2115,2116,2117,2118,2119,2120,2121,2122,2123,2124,2125,2126,2127,2128,2129,2130,2131,2132,... (2 Replies)
Discussion started by: jazz
2 Replies

10. UNIX for Dummies Questions & Answers

dividing the screen >>>

Hi! Is there is any way to divide the screen when I use UNIX shells (I have RedHat 7.1)? I have to see the command promt and process some data from very long file at the same time (I work with PThreads). Some "pseudo-windows" in text mode, huh? =) Thanks in advance and don't be angry =) (4 Replies)
Discussion started by: ShockTeam
4 Replies
Login or Register to Ask a Question