Arithmetic Problem with shell script programming.

Hello everybody,

I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input.

I admit I am really bad at understanding his teaching, for one. He teaches this class as you had previous experience with programming and when i ask him questions he is very evasive with his answers. My problem is that he didn't have a lecture about flowcharts and i don't understand how to make it work for unlimited numbers introduced.

I was able to make a small script that can add 5 numbers from arg and 5 from input but that is all. he told me to use whiles and fors but i cant seem to grasp the logic so now I am asking you for my help.

I will post here his requirements for the script and also the code for the script that i wrote hoping that somebody can help me.
I appreciate all the help and i will check later with you.

Thank you.

General Considerations

1)  Think about the structure of your program before you write any code.

2)  Write your program in small parts.  Test each part before you add the next.

3)  Write your source code in plain ASCII (especially not RTF or DOC).

4)  Include comments with:
      name, assignment, and date (at the top)
      a description of what your program does and how to use it (at the top)
      the logic of your program (if needed, distributed throughout)

5)  Use descriptive names for your variables, functions, etc.

6)  Prompt for interactive input (if applicable).

7)  Test your program with different inputs.


Write a bash program to:

a)  Input zero or more integers from standard input (one per line)

b)  Input zero or more integers from command line arguments

c)  Output the sum of all input numbers on standard output


It is not necessary to check for invalid input or to format the output.

Due midnight 30 November.

and my script

# A small script that can add 5 numbers from the script arguments
# and 5 numbers that can be entered as a user input.

# Usage: Run the program and if no arguments you will be prompted to enter
# 5 numbers from standard input otherwise you can add the 5 arguments and
# then you will be prompted to enter 5 more numbers.

# When there are no arguments the user can enter the numbers
# from standard input and then the addition will be done.
if [ $# -lt 1 ]
then
echo Please enter you numbers \:
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The total is $sum

# When the user enters the numbers as arguments to the script he will
# be prompted to enter numbers as standard input as well and have the
# the addition done.
elif [ $# -le 5 ]
then
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The grand total is $(( $sum + $1 + $2 + $3 + $4 + $5 ))
fi

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.