Question

In: Computer Science

Write a short bash script that takes in two arguments from a user, asks the user...

Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

To Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.

The required script is given below -

Explanation -

  1. In this code there are four functions for each operation which calculates the result in result variable and returns it back
  2. User enters two numbers num1 and num2 along with the operation "+", "-", "*" and "/".
  3. The $oper variable is compared against the four operators and calculates result to which it matches.
  4. In every if condition branch the function is called as Sum $num1 $num2 where $num1 and $num2 are parameters to the function. Inside sum the parameters are fetched by $1 and $2 where $1 is $num1 and $2 is $num2 .

#!/bin/bash
# Code to ask numbers from user and perform operations like, +,-,*,/
#function to add
Sum(){
   result=$(echo "$1 + $2"|bc)
   return $result
}
#function to subtract
Subtract(){
   result=$(echo "$1 - $2"|bc)
   return $result
}
#function to multiply
Multiply(){
   result=$(echo "$1 * $2"|bc)
   return $result
}
#function to divide
Division(){
   result=$(echo "$1 / $2"|bc)
   return $result
}
#reading two numbers and operation
read -p "Enter first number : " num1
read -p "Enter second number : " num2
read -p "Enter the operation : " oper
result=0
#checking for operations and calling the corresponsing function
if [ "$oper" = "+" ]
then
   Sum $num1 $num2
   result=$?
   echo "The sum is : $result "
fi
if [ "$oper" = "-" ]
then
   Subtract $num1 $num2
   result=$?
   echo "The Difference is : $result "
fi
if [ "$oper" = "*" ]
then
   Multiply $num1 $num2
   result=$?
   echo "The Multiplication result is : $result "
fi
if [ "$oper" = "/" ]
then
   Division $num1 $num2
   result=$?
   echo "The result of division is : $result "
fi

Refer to the screenshot below for code and output -

Output -


Related Solutions

Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin.
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
MATLAB Write a script which asks the user of the program to provide an initial horizontal...
MATLAB Write a script which asks the user of the program to provide an initial horizontal position, initial vertical position, initial velocity, and angle. Create a time vector spanning from zero seconds to 100 seconds incremented at 0.01 seconds. Call the function that you created in the previous problem to calculate the trajectory and velocities of the projectile. Find the maximum height of the projectile and the time at which it reaches that point. Write a neat sentence stating what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT