The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Dec 2008
Posts: 56
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 (permalink)  
Old 11-30-2009
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,609
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 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Dec 2008
Posts: 56
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 (permalink)  
Old 11-30-2009
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,609
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 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Dec 2008
Posts: 56
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 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
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 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Dec 2008
Posts: 56
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

Thanks for all your reply...
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Dividing single file by lines? anoc Shell Programming and Scripting 1 04-08-2008 02:08 PM
scipt dividing strings /reg expr maco_home UNIX for Dummies Questions & Answers 3 08-30-2007 10:39 AM
Dividing float values shash UNIX for Dummies Questions & Answers 3 01-17-2007 05:37 AM
dividing a file into two jazz High Level Programming 2 10-20-2006 08:55 AM
dividing the screen >>> ShockTeam UNIX for Dummies Questions & Answers 4 11-06-2001 08:29 PM



All times are GMT -4. The time now is 09:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0