Question

Linux 2)   Write a bash script which when executed, expects two integers, say n1 and n2....

Linux

2)   Write a bash script which when executed, expects two integers, say n1 and n2. If the first argument, n1, is greater than the second argument, n2, the script calls a function (written within the script) and passes to the function n1 and n2 to compute the sum from n1 to n2. This function returns the sum which the script prints. If n1 is not greater than n2, the script calls the same function and passes the n1 and n2 to compute the sum from n2 to n1, and the script prints the sum returned by the function. Below is the expected dialog or interface

    $ ./script9
    ERROR: The script expects two arguments.

    $ ./script9 3 6
    The sum from 3 to 6 is 18.

    $ ./script9 3 3
    The sum from 3 to 3 is 3.

    $ ./script9 6 3
    The sum from 3 to 6 is 18.

Homework Answers

Answer #1

Program:

#!/bin/bash

airth_sum(){
n1=$1
n2=$2
if [ "$n1" -gt "$n2" ];then
sum=$((n1*(n1+1)/2-(n2-1)*n2/2))
elif [ "$n1" -lt "$n2" ];then
sum=$((n2*(n2+1)/2-(n1-1)*n1/2))
else
sum=$n1
fi
echo "$sum" #Whatever is printed is stored in sum_calc variable, by default shell return will return the status of execution
}

#Condition to evaluate the no of arguments
if [ "$#" -ne 2 ];
then
echo "ERROR: The script expects two arguments."
exit 1;
fi
n1=$1
n2=$2
sum_calc=$(airth_sum $n1 $n2)
if [ "$n1" -lt "$n2" ];then
large_no=$n2
small_no=$n1
else
large_no=$n1
small_no=$n2
fi
echo "The sum from $small_no to $large_no is $sum_calc"

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
This code is in C++ Write a function named printMultTable that takes 2 integers, numValues and...
This code is in C++ Write a function named printMultTable that takes 2 integers, numValues and factor, and prints the first numValues greater than 0 that are multiples of  factor, separated by SPACE. It has no return value. printMultTable(5, 4) should print the first 5 multiples of 4, separated by a space, which is "4 8 12 16 20" printMultTable(3, 6) should print the first 3 multiples of 6, separated by a space, which is "6 12 18"   
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT