help writing script file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help writing script file
# 15  
Old 03-10-2004
help correct a problem

I'm trying to find the average of 3 numbers but it doesn't seem to work. Here is what I have so far, can someone point out my mistake or show me what I need to do.

Thanks


ksh-2.04$ cat>Average

echo " Enter 1st Number "

read num1

echo " Enter 2nd Number "

read num2

echo " Enter 3rd Number "

read num3

if ((total=num1+num2+num3/3))

echo "The Average Of The 3 Numbers Is"

fi

exit
# 16  
Old 03-10-2004
Data

Sorry guys, I ment to post this on my first post, didn't mean to start another thread. My mistake. The question was ment for the Help with script file thread.

merged --oombera

Last edited by oombera; 03-10-2004 at 02:40 AM..
# 17  
Old 03-10-2004
First off, your "if" statement is incorrect.. you should have:
Code:
if ((total=num1+num2+num3/3)); then

Secondly, you didn't actually tell the script to display the total! Smilie

And thirdly, you need to obey the orders of operation... your arithmetic operation will only divide "num3" by 3.

However, I don't see a reason to use the "if" statement, which makes life even easier! Smilie
Code:
((total=(num1+num2+num3)/3))
echo "The Average Of The 3 Numbers Is" $total

However, I doubt this is the solution you want ... the result stored in $total is just the integer with anything after the decimal point cut off.

Last edited by oombera; 03-10-2004 at 02:52 AM..
# 18  
Old 03-10-2004
Thanks oombera, this is what I got so far, looks about right but I'm not sure. Math is clearly not my strong point. I added 3 + 3 + 3 and devided it by 3 and got 3. I know I'm supposed to get 7. I don't understand what I'm doing wrong. It's so fustrating!! Smilie

ksh-2.04$ cat>Average
echo " Enter 1st Number "

read num1

echo " Enter 2nd Number "

read num2

echo " Enter 3rd Number "

read num3

((total=(num1+num2+num3)/3))
echo "The Average Of The 3 Numbers Is" $total

exit


ksh-2.04$ sh Average
Enter 1st Number
3
Enter 2nd Number
3
Enter 3rd Number
3
The Average Of The 3 Numbers Is 3
ksh-2.04$

Smilie
# 19  
Old 03-10-2004
Kids these days need a calculator for everything.

ksh has it right. You need to review your math.

3+3+3=9

9/3=3

It would take 7 3's to get what you expect.
3+3+3+3+3+3+3=21
21/3=7
# 20  
Old 03-10-2004
I'm a total dumba$$. Thanks for the help, I feel like a total tool. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Writing File Restore Script

Hi all, I have been tasked with creating a script that sends a file into a created "recycling" directory and another script that restores a "deleted" file. I have already created the removal script but am stuck on the restoring part. I need to restore the file to its original location by... (0 Replies)
Discussion started by: bashbeginner
0 Replies

2. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

3. Shell Programming and Scripting

writing the main script file

Hi, I am new to shell scripting,and i was planning to write a script that will move files which have a datetime >= currentdate-N from a source to destination folder. All configuration should be done through a properties files. Here the value of N should be taken as 10 days(modification... (6 Replies)
Discussion started by: rahul125
6 Replies

4. Shell Programming and Scripting

Need help in writing a script to edit a file

Hi all, I need help in writing a script to edit a file Here is the sample of my file abc xxx 123 456 789 045 def yyy 987 678 098 cdf zzz 435 543 jhg vvv 987 765 (2 Replies)
Discussion started by: leo.maveriick
2 Replies

5. Shell Programming and Scripting

Help with writing shell script file

I am trying to prompt the user using tput command to read the information ( 5 last names, first names and grades) from the keyboard. Save the data in a file called student.txt. Sort the file by last name and display it on the screen My pseudocode is as follow: Pseudocode: Initialize... (1 Reply)
Discussion started by: jestaton
1 Replies

6. UNIX for Dummies Questions & Answers

Help writing a script to find a file

I just started learning about Unix and I cant figure out what im doing wrong. I'm trying to write a script that will ask for the file name and tell what type it is. This is what i have so far. http://i63.photobucket.com/albums/h123/wacand/untitled.jpg (2 Replies)
Discussion started by: wacand
2 Replies

7. UNIX for Dummies Questions & Answers

HELP! writing shell script with c++ file

how would i write a shell script to count number of one-line comments in a c++ file. please help with coding thank you. (1 Reply)
Discussion started by: deadleg
1 Replies

8. Shell Programming and Scripting

script for writing to a file

Hi, Can some some give ideas/help how to write to a file. i need to create a calender from the inputs given on command line i.e frm date,todate & -i is interval is given to write to a file. -i is 1 then a calender is daily , if -i =2 then calender is alternate day e.g $1 ... (0 Replies)
Discussion started by: innocent
0 Replies

9. Shell Programming and Scripting

writing script file for database

how to disable a constraint type of a field (present in diffrent tables) in the database unix a unix script file.. (3 Replies)
Discussion started by: shaksing
3 Replies

10. Shell Programming and Scripting

Writing to a file within a script

Hi, At the moment i have a script where it asks the user if they want to create a file and what to put in the file. The problem is when the script is run the user inputs the information, though when they are finished typing what they want to be in the file there is no way for the program to know... (3 Replies)
Discussion started by: Jaken
3 Replies
Login or Register to Ask a Question