 Unix Handson - 3

3. Unix : Find sum of even numbers

Write a shell script to find the sum of all even numbers from a list of given numbers. The script should first of all take the count of numbers to be added as user input followed by the numbers one by one.

The output should print the following :

Total = <Sum>

Console Input  :

The input needs to be provided as -

The first line contains the count of numbers to be added.

The second line contains the 1st number to be added.

The third line contains the 2nd number to be added. and so on.

For example,

if we want to provide 10, 20 and 30 as the numbers then provide the input as

3

10

11

30

The output for this  example will be

Total = 40

Sample Test Cases

1.

Input :

4

20

10

5

5

Output :

Total = 30

2.

Input :

2

50

11

Output :

Total = 50

Solutions:

awk '
BEGIN {
sum=0;
}
{ if ( \$0%2==0 ){
sum+=\$0;
}
}
END { print "Total","=",sum}'

To view the code in compiler click the below link:

https://onlinegdb.com/ByTbTD7PU