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"
答案1
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"