-1

I'm getting this above syntax error and i can't decipher what is wrong with it. I'm trying to do a float value in BASH.

Hence i used this command called the awk to achieve the target.

while $empty; do
    empty=false
    echo -n "Price : "; read getPrice

#float value using awk
awk 'BEGIN{if ('$getPrice'>'0') exit 1}'
    if [ $? -eq 1 ]; then
    PRICE[$COUNT]=$getPrice;

else
    empty=true 
    echo "Please put in the correct price figure!"
fi

done

However, i got this error

awk: line 1: syntax error at or near >

This error occured when i did not input any value into the getPrice variable. However, it's working fine when i input some value which is >0. After much deliberation, i still could not figure what is wrong with the syntax. Regards.

  • Apart from the awk issue, $COUNT will always evaluate to "" – FloHe Jan 21 '17 at 17:06
2

The reason you get a syntax error in your Awk script is because when $getPrice is empty, then the script is actually just

BEGIN{if (>0) exit 1}

The proper way to import a shell variable into an Awk script as an Awk variable is by using -v:

awk -vprice="$getPrice" 'BEGIN { if (price > 0) exit 1 }'

I also had a look at the flow of contol in your script, and rather than using the $empty variable, you could just exit the loop when a correct price has been entered:

while true; do
    echo -n "Price : "
    read getPrice

    # compare float value using awk
    if awk -vprice="$getPrice" 'BEGIN { if (price <= 0) exit(1) }'
    then
      PRICE[$COUNT]="$getPrice"
      break
    fi

    echo "Please put in the correct (positive) price figure!"
done

Additional detail after comments:

The sure should be alerted about invalid input if the value entered is not a number or if it's negative. We can test the inputted value for characters that are not supposed to be there (anything other than the digits 0 through to 9 and the decimal point):

while true; do
    echo -n "Price : "
    read getPrice

    if [[ ! "$getPrice" =~ [^0-9.] ]] && \
        awk -vprice="$getPrice" 'BEGIN { if (price <= 0) exit(1) }'
    then
        PRICE[$COUNT]="$getPrice"
        break
    fi

    echo "Please put in the correct (positive) price figure!"
done
  • it seems to return me a bc: invalid option -- 'e' error. – Teo Chuen Wei Bryan Jan 21 '17 at 17:12
  • @TeoChuenWeiBryan Ah, you're on Linux. Use bc <<<"$getPrice > 0" instead of bc -e "$getPrice > 0" -e quit (which works on BSD). – Kusalananda Jan 21 '17 at 17:22
  • i seems to get a syntax error when i use the bc <<<"$getPrice > 0" code. May i ask what does <<< do? – Teo Chuen Wei Bryan Jan 21 '17 at 17:39
  • @TeoChuenWeiBryan It's a "here-string". It sends the string inte the command on standard input. I'm starting to think that using bc is a bad idea. Partly because of compatibility reasons, but it also suffers from the same problem as your original script if $getPrice is empty. My Awk solution should still be good though. – Kusalananda Jan 21 '17 at 17:55
  • sorry for the late reply. however i would like to ask, is it possible to do two different check during a single awk script? I'm trying to check if the value is positive and if the user insert any input apart from numbers. like if the user enter a letter, it will prompt any error msg. – Teo Chuen Wei Bryan Jan 30 '17 at 14:59
2

If $getPrice is empty, this line

awk 'BEGIN{if ('$getPrice'>'0') exit 1}'

becomes

awk 'BEGIN{if (>0) exit 1}'

which is probably not what you intended. You could use one of the shell substitutions to provide a non-empty value (though mixing shell and awk like this still is not recommended practice):

awk 'BEGIN{if ('${getPrice:-0}'>'0') exit 1}'

It's better to pass variables to awk using the -v option (but that's actually a different question, answered several times).

  • i tried using the method you gave above, but however if i did not input any value into the price, it doesn't prompt the error message. Instead it continues to run the next function. – Teo Chuen Wei Bryan Jan 21 '17 at 17:38
  • That's expected since the comparison is "0>0" (always false). You'll have to decide what the expected behavior of the script should be, and how to achieve that. – Thomas Dickey Jan 21 '17 at 17:50
0

If there is nothing in getPrice then the awk code is

BEGIN{if (>0) exit 1}

which is totally invalid syntax. Guard against getPrice being empty (or better yet check that it appears to be a number of some sort).

Is bash actually a requirement? This would be easier in ZSH or really any language with floating point support:

bash-3.2$ exec zsh
% echo 3.14 | read x; [[ $x -gt 0 ]] && echo positive
positive
% exec tclsh
tclsh> set x [read stdin]
3.14
3.14

tclsh> if {$x > 0} { puts positive }
positive
tclsh> 
  • yeah, apparently bash is a requirement. i just started learning how to do shell-scripting not long ago. – Teo Chuen Wei Bryan Jan 21 '17 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.