Thanks for reply. Here is the working script. You can see commented section referenced in first post; I'd like to consolidate:
#!/bin/sh
x=0
echo 'Enter number:'
read x
for i in $((x*x)) #&& [ rem in $((((i % 2)) | bc)) ]
do
echo "Square of $x is $i."
done
for rem in $((((i % 2)) | bc))
do
if [ $((i % 2)) -eq 0 ]; then
echo "$i is even, because the remainder of $i divided by 2 is 0."
else
echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."
fi
done
The for construct is used to iterate through a list or repeatedly evaluate an expression. Using it for one single value is at least, hmmm, questionable, as the loop variable is set to exactly that single value.
x=0
echo 'Enter number:'
read x
i=$((x*x)) #&& [ rem in $((((i % 2)) | bc)) ] # integer arithmetics.
echo "Square of $x is $i."
rem=$((i % 2)) # bash can do integer arithmetics only, so NO bc needed...
if [ $rem -eq 0 ]
then echo "$i is even, because the remainder of $i divided by 2 is 0."
else echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."
fi
Thanks again RudiC, I understand now that loop is questionable for single value.
x=0
echo 'Enter number:'
read x
i=$((x*x)) #&& [ rem in $((((i % 2)) | bc)) ] # integer arithmetics.
echo "Square of $x is $i."
rem=$((i % 2)) # bash can do integer arithmetics only, so NO bc needed...
if [ $rem -eq 0 ]
then echo "$i is even, because the remainder of $i divided by 2 is 0."
else echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."
fi