I have tried a program using function. My code is:
#!/bin/zsh
odd=()
prime=()
isprime()
{
for (( i=2; i<=`expr sqrt($1)`; i++))
do
if [ `expr $1 % $i` -eq 0 ]
then
c=`expr $c + 1`
fi
done
if [ $c -eq 0 ]
then
prime[$1]=$1
fi
}
echo -n "Even = "
for (( i=$1; i<=$2; i++))
do
if [ `expr $i % 2` -eq 0 ]
then
echo -n "$i "
else
odd[$i]=$i
fi
isprime $i
done
echo
echo "Odd = ${odd[*]}"
echo "Prime = ${prime[*]}"
On execution I get the following error message:
Even = 2 isprime:2: unknown file attribute: 2
isprime:2: bad math expression: operand expected at end of string
Can someone please tell me what is wrong with this code?