I am getting a syntax error at line 17 : unexpected token `else'
declare -i A
echo "enter any numeric value"
read value
if [$value > 0];
if [[ "$value" =~ ^[0-9]+$ ]]; then
A=$value
else
if ! [[$value =~ ^[0-9]+$ ] || $value !=0];then
A=$[RANDOM%20+1]
fi
fi
else
A=$((RANDOM%25+16))
fi
echo"the value of |A| is $A"
if
needs athen
. Download fromshellcheck.net
and save yourself much trouble in the future. – Paul_Pedant Nov 20 '20 at 13:32[[
and ended it with]
. – Paul_Pedant Nov 20 '20 at 13:34>
is not a "greater than" test. It tests for sort order. You probably meant[ "$value" -gt 0 ]
. You also seem to mix the arcane and obsolete$[ ... ]
syntax for arithmetic expansions with the modern$(( ... ))
syntax. There is a general issue with whitespaces in your code, consider re-writing and testing while doing so. As others have pointed out, the shellcheck.net website will be of use to you. – Kusalananda Nov 20 '20 at 13:36if ! [[$value =~ ^[0-9]+$ ] || $value !=0];then
meant to be? It looks to me the condition will end up falsy (after the outer negation) regardless of the value of$value
, so the main body of theif
will never run. – ilkkachu Nov 20 '20 at 14:30$value
will never be zero (due to the earlier test in the code), so$value != 0
can be removed, leavingif ! [[ $value =~ ^[0-9]+$ ]]
. This would be true for any string that evaluates to an integer for the$value -gt 0
test, but that contains characters other than digits, e.g.12aa
. What was actually meant from the start is unknown, but the whole script could possible have wanted to test for negative, zero, and positive values of$value
, to then setA
depending on this. – Kusalananda Nov 20 '20 at 14:51$value !=0
true. The whole[[ ... || $value != 0 ]]
will be true, and the! [[ ... ]]
will be false. Note that it's||
there, not&&
. – ilkkachu Nov 20 '20 at 16:23