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

Write a shell script called :addall: that takes a list of integers as command li

ID: 660360 • Letter: W

Question


Write a shell script called :addall: that takes a list of integers as command line arguments and displays their sum. The shell also appends this list and sum as one line containing list:sum to the file addall.hst (a history file). The script will also accept the option -hN where N is the numner of lines, and display the last N lines from the history file to the user. Turn in your source and test runs of the shells similar to the following: $ allall Usage: addall [-h] number-list $ addall 5 6 7 The sum of the given 3 numbers is 18 $ addall 10 20 30 40 The sum of the given 4 numbers is 100. $ addall -h 2 1 2 3 4 5 The sum of the given 5 numbers is 15. 10 20 30 40 : 100 1 2 3 4 5 :15 $ addall -h 3 5 6 7 : 18 10 20 30 40 : 100 1 2 3 4 5 :15 $ addall -x 3 Invalid Option -x

Explanation / Answer

#! /bin/sh
total=0 # this is to store total of all numbers
count=0
for element in "$@" # fetching al numbers from list to add
do
total=`expr $total + $element`
count=$count+1
done
printf "The sum of the given %s numbers is : %s" $count,$total #printing total
$ watch tail -n $1 logfile.txt #printing lines from history as per user wish

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote