Computing.Net > Forums > Linux > scripting question...

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.

scripting question...

Reply to Message Icon

Name: mista
Date: November 27, 2008 at 13:21:53 Pacific
OS: xp pro
CPU/Ram: amd
Comment:

hey guys...having some issue using vmplayer for a unix class, and i really suck at scripts...can someone help me figure this out.

#!/bin/Project 5 2
# 22 November 2008
# Created by Charles Clay
# This program adds five numbers entered on the command line together

# Check to see if five numbers were entered

if [[ $# -eq 5 ]]
then

# Display the numbers entered
echo "The first number is: "$1
echo "The second number is: "$2
echo "The third number is: "$3
echo "The fourth number is: "$4
echo "The fifth number is: "$5

# Add the numbers together and divide by 5 to display average
((myaver=$1+$2+$3+$4+$5/5))

echo "Your average is: "$myaver

if [[ $myaver -ge 0 && $myaver -le 68 =$F ]]
then
echo "Your average is a F"
fi
echo "Your average is: "$myaver

if [[ $myaver -ge 68 && $myaver -le 73 ]]
then
echo "Your average is a D"
fi
echo "Your average is: "$myaver

if [[ $myaver -ge 73 && $myaver -le 79 ]]
then
echo "Your average is a C"
fi
echo "Your average is: "$myaver

if [[ $myaver -ge 79 && $myaver -le 91 ]]
then
echo "Your average is a B"
fi
echo "Your average is: "$myaver

if [[ $myaver -ge 91 && $myaver -le 101 ]]
then
echo "Your average is an A"
fi

else

echo "Please enter only five numbers"
echo "Usage: $0 value1 value2 value3 value4 value5 /5"
exit 0

fi

# End script

It calculates the wrong average and says that it can not find




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 27, 2008 at 21:51:56 Pacific
Reply:

First, you are performing the aritmetic incorrectly; it should be:

myaver=$(( ($1+$2+$3+$4+$5)/5))

Keep in mind that the bash shell only does integer arithmetic. In our example, any result not divisible by 5 will be truncated.

If you need more accuracy, use a tool like bc or awk to do the arithmetic. Let me know if you want an example.


0

Response Number 2
Name: wollie
Date: November 28, 2008 at 02:17:37 Pacific
Reply:

mista,

you should at least add some brackets to your calculation:

((myaver=($1+$2+$3+$4+$5)/5))

cheers

Wollie


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


A script to download all ... setting up and using wine...



Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: scripting question...

Shell Script Question www.computing.net/answers/linux/shell-script-question/15096.html

Easy shell script question? www.computing.net/answers/linux/easy-shell-script-question/19758.html

scripting question (mozilla) www.computing.net/answers/linux/scripting-question-mozilla/26788.html