Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bash

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to write a bash script that reads a list of number until 999 is read, and then prints the total of all the number together. This what I got so far can someone tell me what am I doing wrong.

#!/bin/bash
Echo "Enter the Num:"
read num
sum = 0
while [ sum -ne 999 ];
do
let sum + sum = num
echo "Enter another Num"
read num
done


 
based on your description you should be checking the value of num -ne 999 not sum -ne 999. also sum is a program name i suggest using a different name.

not sure of efficiency but the following works

echo "Enter the Num:"
read num
nsum=0
while [ $num -ne 999 ] ; do
nsum=$[$nsum + $num]
echo "Enter another Num"
read num
done
echo $nsum

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top