Home » Infrastructure » Linux » How to trim variable
How to trim variable [message #260981] Tue, 21 August 2007 07:56 Go to next message
shilpa.rajput
Messages: 31
Registered: May 2006
Location: Pune
Member

Dear All,
Do anyone hv idea how to trim a variable in linux?

Actaully , I'm a diverting sqlplus output to a variable and then comapring it with a number

HITRATIO=`sqlplus -s shilpa/shilpa123@PWC1 <<EOF
set heading off
set feedback off
SELECT ltrim(round((1 - (sum(GETMISSES) / sum(GETS)))* 100,2))
FROM V\\$rowcache;
EOF`

if [ $HITRATIO -gt 90 ]
then
.........

But it's throwing ;

-bash: [: 97.59: integer expression expected error

Just drew the conclusion that the output of the sqlplus must be hving space and even varified it by keeping some
characters around it(i.e . HITRATIO variable) to gv the following output :


uyi
97.59hjk


That clearly showed the space. But now how to trim it?

Thanks in advance.
Re: How to trim variable [message #260990 is a reply to message #260981] Tue, 21 August 2007 08:08 Go to previous messageGo to next message
Michel Cadot
Messages: 59087
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
In SQL*Plus: "set trimout on"
Outside: "sed"

Regards
Michel
Re: How to trim variable [message #261069 is a reply to message #260990] Tue, 21 August 2007 13:41 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
I don't think you are getting this error because of space in your output. You are getting this error because shell is expecting an integer, but the output is not an integer. Also you don't need ltrim

Linux shell and most of the shells doesn't like floating numbers. To get around this problem replace the round function with ceil. Something like
Quote:
$ cat test.sh
HITRATIO=`sqlplus -s rnd/rnd <<EOF
set heading off
set feedback off
SELECT ltrim(round((1 - (sum(GETMISSES) / sum(GETS)))* 100,2))
FROM V\\$rowcache;
EOF`
echo $HITRATIO
if [ $HITRATIO -gt 90 ];
then
echo "issue"
else
echo "no issue"
fi

HITRATIO=`sqlplus -s rnd/rnd <<EOF
set heading off
set feedback off
SELECT ceil((1 - (sum(GETMISSES) / sum(GETS)))* 100)
FROM V\\$rowcache;
EOF`
echo $HITRATIO
if [ $HITRATIO -gt 90 ];
then
echo "issue"
else
echo "no issue"
fi
$sh test.sh
89.72
test.sh: line 8: [: 89.72: integer expression expected
no issue
90
no issue



Regards

Raj
Re: How to trim variable [message #261135 is a reply to message #261069] Tue, 21 August 2007 23:20 Go to previous message
shilpa.rajput
Messages: 31
Registered: May 2006
Location: Pune
Member

This really has added to my knowledge.
I'm quite new to Linux shell fundas.
Thanks a lot for the solution. It worked Smile
Previous Topic: How to hide terminal output in Linux
Next Topic: Invisible parametr passing in Linux
Goto Forum:
  


Current Time: Tue Sep 16 12:56:49 CDT 2014

Total time taken to generate the page: 0.08426 seconds