This assignment continues on from the work from the last lab. You will only work
ID: 3829411 • Letter: T
Question
This assignment continues on from the work from the last lab. You will only work in Linux in this lab. Start your Linux VM, open two terminal windows and cd to ~/scripts in both. a. In one terminal window, type vi script3.sh and enter the following code (use i to enter insert mode): #!/bin/bash Sum = 0 for k in $@; do Sum = $ ((Sum + k)); done echo The sum of the parameters is $sum Type to exit insert mode, :w to save the file and in your other terminal window remember to change the file's permissions to be executable. Run this script by typing ./script3.sh 15 12 20 38 16. What is output? Explain how this script works. b. Modify script3.sh to compute and output the average of the list of parameters by computing the average using Avg = $ ((Sum/$#)). Output Avg when done. Run this with the above list of numbers. Does it work? Explain what $# refers to and how this new instruction works. c. Run the script from part 2 but with no parameters. What happens? Why? In order to prevent this error, we will use an if-then-else statement. Add the statement if[$# -eq 0]; then echo Error, cannot compute an average of 0 numbers; else... fi using your previous Avg = ... and echo $Avg instructions from step 1b in place of the Note that you do not have to place this statement on four or more separate lines, it can all go on one line if desired although in this format, it is more readable. Save and run the script with several parameters and again with no parameters. Once it is working correctly, use this final version of the script as your answer to step 3. Explain what the if-then-else statement does why it is good to include it.Explanation / Answer
(A)The output: The sum of the parameters is 101.
This script works by taking the command line arguments using $@(it contains all the command line arguments) in the for loop and calculating the sum of all the command line arguments.
(B)The output: The average of parameters is 20
Ran the script, it worked but we are not getting the exact average here the exact average is 20.2 but it displayed 20 which is expected as it rounded off.
Here $# gives the number of command line parameters passed to the script.
(C)If you run the script without parameters division by zero occurs. Since $# is 0
Output:
The sum of the parameters is 0
script3.sh: line 6: sum/0: division by 0 (error token is "0")
If else statment check whether if number of command line arguments are zero or not if zero it displays it cannot compute avg of zero numbers.
else it computes the average.
It is good to include because the division by zero error is handled and the user will let get to know the error in input.
Program:
#!/bin/bash
sum=0
avg=0
for k in $@; do sum=$((sum+k));done
echo "The sum of the parameters is " $sum
if [ $# -eq 0 ];
then
echo "Sorry, cannot compute an average of 0 numbers"
else
avg=$((sum/$#))
echo "The average of parameters is " $avg
fi
Output:
linux1:~ # ./script3.sh
The sum of the parameters is 0
Sorry, cannot compute an average of 0 numbers
linux1:~ # ./script3.sh 15 12 20 38 16
The sum of the parameters is 101
The average of parameters is 20