G.K.K
October 15, 2008, 7:59am
1
Hi,
i am facing an error in the following script in the korn shell but in bash it is working , can any help me to convert the below script to korn shell script without errors.
echo 123.4566 | bc -l | awk -F '.' '\{ print $1; exit; \}'
Thanks in advance
kamal
I got 123 as an answer, no error.
I ran it on HP-UX in a script starting with #! /usr/bin/ksh
Ans also on the commandline were echo $SHELL was the ksh.
What error do you get?
G.K.K
October 15, 2008, 8:35am
3
the form me it has shown was ,
awk: syntax error near line 1
awk: bailing out near line 1
$ echo 123.4566 | bc -l | awk -F '.' '{ print $1; exit; }'
awk: syntax error near line 1
awk: bailing out near line 1
$ echo 123.4566 | bc -l | awk -F. '{ print $1 }'
123
And, of course, all you need is:
$ typeset -i n=123.4566;print $n
123
G.K.K
October 15, 2008, 9:13am
5
radoulov:
$ echo 123.4566 | bc -l | awk -F '.' '{ print $1; exit; }'
awk: syntax error near line 1
awk: bailing out near line 1
$ echo 123.4566 | bc -l | awk -F. '{ print $1 }'
123
And, of course, all you need is:
$ typeset -i n=123.4566;print $n
123
how to round it to near value....
if it is 123.999, then how to make it 124 using scripting in ksh
It seems you're using ksh88 (a Korn shell implementation that does not support floating point arithmetic).
So you can try something like this:
$ unset n
$ n=123.333
$ _n=${n#*.};((${_n%${_n#?}}>=5))&&print $((${n%.*}+1))||print ${n%.*}
123
$ n=123.666
$ _n=${n#*.};((${_n%${_n#?}}>=5))&&print $((${n%.*}+1))||print ${n%.*}
124