PDA

View Full Version : UNIXX What's wrong with that code!!!


codeffect
11-15-2007, 05:18 PM
Can I use the "modulo" sign : % in unix as if [ `$var%4` -eq 0 ] ?! howw can i evaluate numeric operations?
What's the soluttion of my code ?!



echo "Enter year : "
read var
if [ `$var%4` -eq 0 ]
then
echo "The year is bisextil"
else
echo "The year is not bisextil"
fi

Dunna
11-15-2007, 05:30 PM
What's the soluttion of my code ?!

What are you even asking? Please state what you need more clearly. Is this unix?

codeffect
11-15-2007, 05:39 PM
I am creating a function in the vi and call it from the Shell with ./FunctionName

the function is :


echo "Enter year : "
read var

if [ \( expr $var % 4 \) -eq 0 ]
then
echo "The year is bisextil"
else
echo "The year is not bisextil"
fi


this function iis to know whether the year can be devided by 4 with as 2004 % 4 = 0 return true
and if not return false

oracleguy
11-15-2007, 06:02 PM
Is this unix?

It isn't Unix per-se. That is a bash script, which is a shell that runs on Unix based OSes (like Linux).

As to your problem, you need to do the math correctly which is your problem. expr will work if done correctly or you can do it this way:
if [ "$(($var % 4))" -eq 0 ]

codeffect
11-16-2007, 01:18 PM
Thank you i will try this code.

codeffect
11-16-2007, 02:39 PM
Maybe the % is a special caracter , or maybe it isnt converted to an arithmetic expression

echo "Enter year : "
read var
if [ "$(($var % 4))" -eq 0 ]
then
echo "The year is bisextil"
else
echo "The year is not bisextil"
fi

oracleguy
11-16-2007, 06:34 PM
It works the one Linux computer I tested it on. % is the modulus character in bash, which is what you are trying to do here. So what happened when you ran the code? Help us help you and post any error messages you received.

codeffect
11-18-2007, 02:24 PM
Thank oracle guy The code :
if [ "$(($var % 4))" -eq 0 ]
worked.