Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write and execute a Bourne shell script called hw3-17 that: Requires a list o

ID: 645283 • Letter: 1

Question

1. Write and execute a Bourne shell script called hw3-17 that: Requires a list of numbers as command line arguments. The script will sum the odd arguments. The script should:

a. First display a count of all arguments:

Example:    There are 5 arguments.

    

But if no arguments are listed when the script is run, the output should just be:

                No arguments to process.

                  and the script should exit.

B) Examine each the number that was given as command line argument and determine if it is an odd number. If it is odd, display it and add its value to the odd number sum.          (Hint: Try using a for-in loop)

C) After all the command line arguments have been examined, display the sum of the odd arguments.

For example, the command:
           $ hw3-17 2 53 87 34 96 11

would produce the output:

     There are 6 input arguments.

Odd arguments:
     53

     87

     11

     Sum of the odd arguments is 151.

Note: If there were arguments, but none of them were odd arguments,
the sum displayed should be zero.

Explanation / Answer

echo -n "Odd arguments:"
read n
sum=0
for i in $*
do
rem= $(($n % 2))
if [$rem -eq 0 ]
then
    sum=`expr $sum + $i`
else
echo "Sum of the odd arguments is: 0"
done
echo "There are "$#" arguments"
echo "Sum of the odd arguments is: "$sum