Computing.Net > Forums > Unix > need help basic unix scripting: How

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

need help basic unix scripting: How

Reply to Message Icon

Name: kamakazi109
Date: August 1, 2005 at 13:57:30 Pacific
OS: lunix
CPU/Ram: p4, 265mb
Comment:

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"



Sponsored Link
Ads by Google

Response Number 1
Name: Luke Chi
Date: August 1, 2005 at 16:22:30 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: need help basic unix scripting: How

Need help writing UNIX script www.computing.net/answers/unix/need-help-writing-unix-script/2372.html

need help on Unix program www.computing.net/answers/unix/need-help-on-unix-program/4936.html

help redoing unix script student db www.computing.net/answers/unix/help-redoing-unix-script-student-db/5151.html