-1

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"
codeholic24
  • 307
  • 3
  • 15
Wissi
  • 1
  • 4
  • 5
    The first `if` needs a `then`. Download from `shellcheck.net` and save yourself much trouble in the future. – Paul_Pedant Nov 20 '20 at 13:32
  • 2
    Also see [Why are bash tests so picky about whitespace?](https://unix.stackexchange.com/questions/117438/why-are-bash-tests-so-picky-about-whitespace) – steeldriver Nov 20 '20 at 13:32
  • 1
    shellcheck will also tell you where you started a test with `[[` and ended it with `]`. – Paul_Pedant Nov 20 '20 at 13:34
  • 2
    Also, `>` 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:36
  • It worked and the link was very useful. Thank you. – Wissi Nov 20 '20 at 13:45
  • leaving aside the syntax, what's the logic in `if ! [[$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 the `if` will never run. – ilkkachu Nov 20 '20 at 14:30
  • @ilkkachu In that test, the value of `$value` will never be zero (due to the earlier test in the code), so `$value != 0` can be removed, leaving `if ! [[ $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 set `A` depending on this. – Kusalananda Nov 20 '20 at 14:51
  • @Kusalananda, right, because of the outer test, it'll never be zero, making `$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

1 Answers1

0

Used shellcheck.net to fix syntax issue

Updated code :

#!/bin/bash

declare -i A

echo "enter any numeric value"
read -r value

if [ "$value" -gt 0 ] ; then
    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"
codeholic24
  • 307
  • 3
  • 15
  • 1
    Not that it matters syntactically, but that innermost `if` statement basically says "if `$value` is a zero and at the same time not an integer". – Kusalananda Nov 20 '20 at 15:42
  • @Kusalananda yes i too saw that it's contradict but his issue was to solve unexpected error so i considered that only – codeholic24 Nov 20 '20 at 16:42