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

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

ID: 3859296 • Letter: #

Question

. 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

NOTE: I have completed your assignment and below is the code. Please check and let me know if you face any issues. I will revert back within 24 hours.

Code:
#!/bin/bash

if [ $# -eq 0 ]
then
   echo "No arguments to process."
   echo "Usage: $0 num1 num2 num2...."
   echo "Example: $0 10 15 20 23 30..."
   exit
fi

sum_odd_args=0
echo "There are $# input arguments."
for each_arg in $*
do  
   remainder=`expr $each_arg % 2`
   if [ $remainder -ne 0 ]
   then
       echo $each_arg
       sum_odd_args=`expr $sum_odd_args + $each_arg`
   fi
done
echo "Sum of the odd arguments is ${sum_odd_args}."

Code execution screenshot:
https://pasteboard.co/GC5G4zp.png