echo "enter one no"
read n
rem='expr $n % 2'
if [ $rem -eq 0 ]
then
echo "Number $n is even"
else
echo "Number $n is odd"
fi
while executing this program , bash is showing message like "too many arguement ".
That's because you are doing:
if [ expr $n % 2 -eq 0 ]
How come?
Because
rem='expr $n % 2'
is assigning variable rem
as string expr $n % 2
.
You need, command substitution:
rem=$(expr $n % 2)
Also use bash
's native arithmetic operator instead of expr
:
rem=$(( $n % 2 ))
use read's built in prompt feature read -p "Enter one num" n
prefer using $( . . .)
, single quotes don't expand commands. rem="$(expr $n % 2)"
Quote the variables when using them "$rem"
use at most one line of break between if
and then
if [ "$rem" -eq 0 ]
then