Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
please help: how can i write a script
Example 1:
[min@alpha min]$ ./mt
Enter an integer number:
3
You entered 3
The sum of all the integer from 0 to 3 is 6
The integer 3 that you entered is odd
Example 2:
[min@alpha min]$ ./mt
Enter an integer number:
4
You entered 4
The sum of all the integer from 0 to 4 is 10
The integer 4 that you entered is even
the : mt script must do 3 things:
1) Display which integer the user has entered. For example, if the user enteres 3 then tm script says "You entered 3"
2) If the user enters an integer. For example, 3 then 1+2+3=6, 4 1+2+3+4=10, or 100 1+2+3+...+100=5050.
3) Finally, tm script will have to identify if the user input integer is odd or even and display accordingly. For example, if the user enteres 3 it should display "The integer 3 that you entered is odd" or if the user enteres 4 it should display "The integer 4 that you entered is odd"

I am wondering why do you need to write this program. Is it the homework ? You'd better do it yourself for your own good.
Anyway, the following is a working script:
#!/usr/bin/sh
echo "Enter an integer number: "
read NUM
echo "You entered $NUM"
INDEX=$NUM
TOTAL=0
while [ $INDEX -gt 0 ]
do
TOTAL=`expr $INDEX + $TOTAL`
INDEX=`expr $INDEX - 1`
done
echo "The sum of all the integer from 0 to $NUM is $TOTAL"
if [ `expr $NUM % 2` ]; then
echo "The integer $NUM that you entered is odd"
else
echo "The integer $NUM that you entered is even"
fi
Luke Chi

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |