#!/bin/sh
CURRENTSTATE=2
CSVCSTATE=2
LASTSTATECHANGE=8
CSVCSTATEAGE=5
if (($CURRENTSTATE==$CSVCSTATE))&&(($LASTSTATECHANGE>=$CSVCSTATEAGE))
echo GREAT
fi
returns:
./aff: line 12: syntax error near unexpected token `fi'
./aff: line 12: `fi'
what am i doing wrong here?
sometimes, when the numbers are in notation, i also get an error. how can i fix this?
i get this error:
script.sh: : [: 1.51764e+06: integer expression expected
i want to be able to compare any value to a set number.
like, if 1.51764e+06 is greater than whatever value is in a specific variable, do this or that.
any ideas?
I am confused why you call this thread 'awk' when you're not using awk.
You didn't put a 'then', shell if needs those.
if [ "$NUMBER" -lt "$OTHERNUMBER" ]
then
echo "something"
fi
...but, as you discovered, shell does not support floating point. KSH is the only shell that does.
You can use awk instead, but this is expensive.
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'END { if(!(VAR1<VAR2)) exit 1}' /dev/null
then
echo "var1 < var2"
fi
1 Like
corona688:
I am confused why you call this thread 'awk' when you're not using awk.
You didn't put a 'then', shell if needs those.
if [ "$NUMBER" -lt "$OTHERNUMBER" ]
then
echo "something"
fi
...but, as you discovered, shell does not support floating point. KSH is the only shell that does.
You can use awk instead, but this is expensive.
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'END { if(!(VAR1<VAR2)) exit 1}' /dev/null
then
echo "var1 < var2"
fi
thank you for the response!
the script i'm working on is pretty huge. but here is the part i just need to modify to be able to support a floating point:
if (( ${CURRENTSTE} == ${CSVCSTE} ) && ( ${LASTSTECHANGE} -ge ${CSVCSTEAGE} )) ; then
any one of these variables can have a floating point.
the script is written in bash.
If any one of those variables can have a floating point, you can't use ==. An infintesimal difference or even same number, different representation could make them not equal. How accurate do you want the comparison?
corona688:
If any one of those variables can have a floating point, you can't use ==. An infintesimal difference or even same number, different representation could make them not equal. How accurate do you want the comparison?
this is what i ended up doing, but i'm not sure if it's doing what i want it to:
if awk -v CURRENTSTE="$CURRENTSTE" \
-v CSVCSTE="$CSVCSTE" \
-v LASTSTECHANGE="$CSVCSTEAGE" \
-v CSVCSTEAGE="$CSVCSTEAGE" \
'END { if(!(CURRENTSTATE==CSVCSTE) && (LASTSTECHANGE>=CSVCSTEAGE)) exit 1}' /dev/null ; then
pamu
September 24, 2012, 10:24am
6
can we use above highlighted directly..?
i think this should be
if [[ $(code) ]]
then
#do some here
fi
Yes, this is how if is meant to be used. It checks the return status of the command given.
If in doubt, remember, [ is a command.
$ whereis '['
[: /usr/bin/[
$
Usually a shell builtin, these days, but the command still exists.
1 Like