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.

  1. The problem statement, all variables and given/known data

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.

  1. Relevant commands, code, scripts, algorithms:

Free to use any of if statements, elif statements, while loops, for loops to achieve desired result.

  1. The attempts at a solution (include all code and scripts):
    Unfortunately script accepts only 5 numbers and the professor wants unlimited input.

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

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

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Diablo Valley College, PleasatHill, Ca, Stuart Fogg, Comsc117 I cant add URL if I don't have 5 posts, sorry.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

the synthax of a while loop is for example :

while [ condition ]
do
<some command>
done

by the way, you should also take a look at the += operator so that you can pass the read value as the incremental value of the sum that would be updated "on the fly"

You also could consider the

while :
do
...
done

the colon means /bin/true (this is for making endless loop).
to go out of the while loop, you could use break ( exit would take you out of the script)

You can also have an eye to the trap command so you could handle a scenario like :
the script always ask number (infinite loop)
when the user want to display the sum, he press <Cntrl>+<C> key.
That will raise a INT signal that can be trapped (see trap command) to get out of the wile loop and display the sum.

I could write it but that wouldn't help you to learn, so now i think you have enough clue to go on :wink:

1 Like

your prof wants a loop I think, for the command line part.
There is a problem with arguments you need to know
you can reference $1 .. $9 the way I show. More than that and it becomes ${10} ${11} and so on. I would suggest using the shift command to move each argument into the $1 variable.

you need a loop for the arguments as well

#!/bin/ksh
total=0
while :
do
     echo "enter number, return to stop: \c"
     read num
     if [ -z "$num" ] ; then
           break
     fi
     total=$(( $total + num )) 
done
echo "Total = $total"
1 Like

@jim
I have just been told by bakunin not to provide full solution to people in the course/education section ... :wink:

1 Like

You know I don't want to create any problems to anybody so i do not want the solution what i want is to figure it out how to create unlimited input. I know it may be limited to the operating system limit so I thank everybody for helping.

I will talk to my professor as well, no doubt.

You can do an "unlimited" input just the way jim demonstrate i.e. using an "endless" loop :

while :
do
...
done

but of course you need to foresee some conditions that will break the loop so the user can display the sum of what he has entered.

In Jim code for example, if the user doesn't give an entry and press <enter> then the variable will be empty and the loop breaked.

Thank you guys for all the help,
with all the information that you provided plus my discussion with the professor
I was able to finish the script close to what the professor required.

here is the code for the script

#!/bin/bash
if [[ $# -le 0 ]]
then
echo Please enter you numbers \:
sum=0
while true
  do
  if read num1
    then
    sum=$(( $sum + $num1 ))
  else
    break
  fi
done
echo $sum
fi

if [[ $# -ge 1 ]]
then
count=1
arg=$#
sum=0
while [ $count -le $arg ]
  do
    num1=$1
    sum=$(( $sum + $num1 ))
    shift
    count=$(( $count +1 ))
  done
echo $sum
fi

Could be shorten (1 loop , 1 time sum=$(($sum+$num)), 1 break)

#!/bin/bash
sum=0
flag=$#
while :
do
    case "$flag" in
        0) echo Please enter you numbers \: ; read a ;;
        *) a=$1 ; shift ;;
    esac
    [[ -z "$a" ]] && break
    sum=$(($sum+$a))
done
echo "Total = $sum"
1 Like

ctsgnb ... thank you very much I will look at that. I already turned in the program and i am glad i was able to understand all this. I mean i am completely new at UNIX but i really love the idea using the command line. it is so much fun than using just icons and windows. The more I will learn the more I will be stopping on this forum for discussions. thanks.