Operating System - HP-UX
1833971 Members
1747 Online
110063 Solutions
New Discussion

Re: how do I convert this to a number?

 
SOLVED
Go to solution
Sharvil Desai
Frequent Advisor

how do I convert this to a number?

list="0028 0084 0097"
for var in $list ; do
if $var -gt 0 || $var -lt 200;then
...do stuff...
fi
done
I am getting syntax error (I think its' because $var is not a number) Can one of you experts please help? Thank you!
"help!"
8 REPLIES 8
Sharvil Desai
Frequent Advisor

Re: how do I convert this to a number?

Actually, I have it as
if [ $var -gt 0 && $var -lt 300 ]; then
...do stuff...
fi
And it gives me syntax error.
"help!"
Rodney Hills
Honored Contributor
Solution

Re: how do I convert this to a number?

Try putting the "if" condiction in brackets

if [[ $var -gr 0 || $var -lt 200 ]] ; then

In your example, the shell is trying to execute as a command what's between the if and then.

HTH

-- Rod Hills
There be dragons...
Leif Halvarsson_2
Honored Contributor

Re: how do I convert this to a number?

Hi,
Try
typeset -i var
James R. Ferguson
Acclaimed Contributor

Re: how do I convert this to a number?

Hi:

Change the '&&' to '-a'. See the 'test' man pages. The binary AND operator is '-a'.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: how do I convert this to a number?

If you use single brackets "[" "]", then use "-o" instead of "||" for the "or" operator.

Double brackets accept "||" as "or".

HTH

-- Rod Hills
There be dragons...
Tony Contratto
Respected Contributor

Re: how do I convert this to a number?

Hi Sharvil,

You need to enclose the "$var -gt 0 || $var -lt 200" expression like "[[ $var -gt 0 || $var -lt 200 ]]"

Since the [[ and ]] are considered keywords by the shell, there must be a space between the opening [[ and $var. There must also be a space between 200 and ]].

--
Tony
got root?
Ramkumar Devanathan
Honored Contributor

Re: how do I convert this to a number?

Hi,

I read somewhere in the forums the following -

The difference between using '[[' and '[' to specify the conditions is that while '[[', ']]' are shell builtin operators the '[' and ']' operators are not.

So, for optimal performance use '[[' and ']]' to specify conditions.

- ramd
'as an aside'
HPE Software Rocks!
Sharvil Desai
Frequent Advisor

Re: how do I convert this to a number?

Hi everyone,
Thanks for your prompt replies.
Rodney's response worked just fine. But I also learned new things from everyone else's response. So thank you, and of course points for everyone!
"help!"