Operating System - HP-UX
1834201 Members
2516 Online
110066 Solutions
New Discussion

error in shell script code..ksh

 
SOLVED
Go to solution
amonamon
Regular Advisor

error in shell script code..ksh

I am using ksh..


I calculated variable with bc and if I do echo $g I get: 167.500

I would like to do check for variable $g

in case it has value: 167.500 assign 168 to variable $h..or in case it has value 165.000 assign 165 to variable $h..here is what I did so far:

in=$1
a=`echo "$in/5"|bc`
b=`expr $a + 1`

g=`echo "(($b * 123) / 100)+1" | bc`

if [ $g==*000 ]
then
h=`echo "(($b * 123) / 100)" | bc`
else
h=`echo "(($b * 123) / 100)+1" | bc`
fi

value $h should be one that I look for..

any help would be helpfull..
14 REPLIES 14
Peter Nikitka
Honored Contributor

Re: error in shell script code..ksh

Hi,

the syntax you are using is not correct.
>>
if [ $g==*000 ]
then
<<

You could use a 'case', but if you want a simple rounding to integer of
$i*1.23/5
my suggestion is:

i=$1
h=$(print $i | awk '{printf("%.0f\n",$1*1.23/5)}')
print $h

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
SANTOSH S. MHASKAR
Trusted Contributor

Re: error in shell script code..ksh

Hi,

-------
in case it has value: 167.500 assign 168 to variable $h..or in case it has value 165.000 assign 165 to variable $h
-----------

how( and what do u mean by above) u r going to achive above by using

-------------------
if [ $g==*000 ]
then
h=`echo "(($b * 123) / 100)" | bc`
else
h=`echo "(($b * 123) / 100)+1" | bc`
fi
------------------------

I think u have to round the figure to next integer (eg. 16.500 to 17 and 16.49 to 16)
is it this u want?
amonamon
Regular Advisor

Re: error in shell script code..ksh

ok here is again what I do have:

# in this U enter $1

a=`echo "$1/5"|bc`
b=`expr $a + 1`
c=`echo "$b*5"|bc`


g=`echo "(($b * 123) / 100)+1" | bc`


# so by calculating g goes if statement...

if [ $g==*000 ]
then
h=`echo "(($b * 123) / 100)" | bc`
else
h=`echo "(($b * 123) / 100)+1" | bc`
fi


# this means if for example $g is 167.500 value of h will be:
h=`echo "(($b * 123) / 100)+1" | bc`

and if value od $g is 156.000 vale of h will be:
h=`echo "(($b * 123) / 100)" | bc`

or more examples:

is g is 14.145 h will be:
h=`echo "(($b * 123) / 100)+1" | bc`

or if g is 45.000 h will be:
h=`echo "(($b * 123) / 100)" | bc`

my problem is in syntax of if statement in this:

if [ $g==*000 ]
amonamon
Regular Advisor

Re: error in shell script code..ksh

problem with rounding I want to solve with this is starement

so as it says rounding will be:

(eg. 16.500 to 17 and 16.49 to 17 again)
16.000 will be rounded to 16!..

I know specific rounding..
It is just problem with if stat.to recognise last 3 digits..
//if last 3 digits are 0 if is true:
if [ $g==*000 ] #but this does not work
Peter Nikitka
Honored Contributor

Re: error in shell script code..ksh

Hi,

if you insist on such a construct:
...
case $g in
*000) # if part
;;
## possibly wanted:
#*[024]00) # elif round to floor
#;;
#*[68]00) # elif round to ceiling
#;;
*) # else part
;;
esac

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: error in shell script code..ksh

well it is not that I insist..that was my solution..and it is alllwwayss very nice to hear better solution..

thank U veryyy much
Peter Nikitka
Honored Contributor

Re: error in shell script code..ksh

Hi,

to come back to my first solution:
Really easy for checks (modify the formula as required, if I guessed wrong):
awk '{printf("%.0f\n",$1*1.23/5)}'

Now enter you input values (stdin) and the calculated values you see on stdout.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: error in shell script code..ksh

by the way this case is not working is always reads *) statement

problem is he does never go in *000) check..even is $g is 123.000

amonamon
Regular Advisor

Re: error in shell script code..ksh

Thanks Peter your solution for rounding is OK..but my requirement for rounding is specific..see above post

12.000 rounded: 12
12.043 rounded: 13
23.999 rounded: 24
etc.
Peter Nikitka
Honored Contributor

Re: error in shell script code..ksh

Hi,

then I suggest do look at the value from an integer point of view:

awk '{printf("%d\n",$1*1.23/5)}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: error in shell script code..ksh

whatever..problem is not now with rounding..it is about if or case statment...
after that we can deal with rounding..
Peter Nikitka
Honored Contributor
Solution

Re: error in shell script code..ksh

Sorry,

I did not notice, that you want to round up - my last solution just drops the fractional part.
So better add .5 and use usual rounding:
awk '{printf("%.0f\n",$1*1.23/5+0.5)}'


BTW: the case solution does not work due to the format your '$g' has: I don't see any fractional output in
print $g

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: error in shell script code..ksh

thanks for help guys..
amonamon
Regular Advisor

Re: error in shell script code..ksh

thanks for case statement..:)