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-26-2009
Registered User
 

Join Date: Nov 2009
Posts: 4
Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got

Code:
let value=$1;
while [ magn=1;"$value">\(magn*10\); magn*=10 ]
do
let digit=( $value/magn )%10
if [ $digit >= 5 ]
then
 $value+=( "10"-$digit )*$magn
else
 $value-=( $digit*$magn )
fi
done
echo $value

its supposed to take ./rounders 1447
and output 2000

I posted this in the OS sub forum also. NOOB mistake i know but hey its my first day. SOrry
  #2 (permalink)  
Old 11-26-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
Your script seems to have a considerable disregard for syntax. I suggest you start with some minimal segments of your code and just see if they work.

---------- Post updated at 02:17 ---------- Previous update was at 02:02 ----------

Alright, here you go. I turned it further into a bash/ksh kind of script:

Code:
value=$1
for (( magn=1;value>magn*10; magn*=10 ))
do
  (( digit=(value/magn)%10 ))
  if (( digit >= 5 ))
  then
    (( value+=(10-digit)*magn ))
  else
    (( value-=(digit*magn) ))
  fi
done
echo $value

It doesn't work right, but at least it runs
  #3 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2009
Posts: 4
very cool. Thanks, I will play with it some more and try to get it to work. If i need more help I will post here again. Thanks so much, I was already getting frustrated.
  #4 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2009
Posts: 39
Quote:
Originally Posted by kingrj46 View Post
very cool. Thanks, I will play with it some more and try to get it to work. If i need more help I will post here again. Thanks so much, I was already getting frustrated.

variables cannot contain spaces, and neither should file names.

unix not windows...

actually most languages do not allow this, c/c++ java, to name a few.
  #5 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2009
Posts: 4
Quote:
Originally Posted by gcampton View Post
variables cannot contain spaces, and neither should file names.

unix not windows...

actually most languages do not allow this, c/c++ java, to name a few.
Not sure I follow. I dont think i put any spaces in either. Is there something I missed?
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
Rounding Script Help kingrj46 Linux 0 11-26-2009 03:33 PM
Rounding numbers down dcfargo UNIX for Dummies Questions & Answers 5 09-16-2008 08:14 PM
Rounding off to the next whole number damansingh Shell Programming and Scripting 2 07-17-2008 07:18 AM
Rounding problem shash UNIX for Dummies Questions & Answers 2 01-18-2007 05:33 AM
Rounding off using BC. noodlesoup Shell Programming and Scripting 3 09-11-2006 01:38 AM



All times are GMT -4. The time now is 07:47 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