(Linux) 1. Write shell scripts to solve the following problems: a. Write a shell
ID: 3704086 • Letter: #
Question
(Linux)
1. Write shell scripts to solve the following problems:
a. Write a shell script which will accept up to 9 words as arguments and display each of the words in
uppercase.
b. Write a shell script which will accept an unlimited number of integers as command line arguments.
Your script shall calculate and output the sum of these integers.
c. Write a shell script which will accept two integers as arguments then compute and output their
product. Display an appropriate error if two arguments are not provided.
Explanation / Answer
1)
# Changing all the command line argument to upper case.
echo $@ | tr '[a-z]' '[A-Z]'
b)
# Initializing the value of sum to zero
sum = 0
for i in $@; # Passing through each argument
do
sum = $sum + $i # Summing the argument
done
echo "$sum" # Displaying the sum of the arguments
c)
# Checking number of arguments supplied
if [ $# -ne 2 ]; then
echo -e " Please provide correct number of arguments"
else
# $1 is the first argument and $2 is the second
echo " Sum of $1 + $2 is `expr $1 + $2`"
fi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.