I am using below script
x=5.44
p=0
temp=$(printf "%.*f\n" $p $x)
echo $temp
if [ temp -gt 0 ]
then
echo "inside"
fi
and I am getting below output with error.
5
./temp.sh: line 6: [: temp: integer expression expected
You need to use $
for the shell to expand temp (As your script is written you are trying to compare the literal string temp
to the integer 0
). You also should quote it:
x=5.44
p=0
temp=$(printf "%.*f\n" $p $x)
echo "$temp"
if [ "$temp" -gt 0 ]
then
echo "inside"
fi
If you are using bash a better way to do it would be using bash arithmetic expression like so:
x=5.44
p=0
temp=$(printf "%.*f\n" $p $x)
echo "$temp"
if ((temp>0)); then
echo "inside"
fi
Inside of the arithmetic expression ((…))
you do not need a $
for expansion and you cannot quote.
printf
has a-v
option to store the result in a shell variable to avoid the need for command substitution like this.printf -v temp ...
is equivalent totemp=$(printf ...)
but carries much less overhead than the latter.