Question

In: Computer Science

Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...

Bash Script

Write a script using while-do-done loop to convert the kilometers to miles.

- Ask the user to input the number of kilometers they need to travel.

- Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137

- Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop and print “Thank you for using this application”.

- If the user enters any other character, then display/print “Invalid entry” and break the loop.

Solutions

Expert Solution

Bash Script:

#!/bin/bash

# Bash Script to convert miles to kilometers

conv=0.62137

# Loop till user want to stop
while [ true ]
do
   # Reading Kilometers
   read -p 'Enter number of kilometers they need to travel: ' kms  

   # Converting to miles
   miles=$(expr $kms*$conv | bc)

   # Printing results
   echo "Miles: " $miles

   # Printing option
   printf '\nDo you want to continue? '
   read choice

   # Responding to corresponding option
   case $choice in
   'Y')
       # Continuation
       ;;      

   'y') # Continuation
       ;;  
      
   'N')  
       echo "Thank you for using this application"
       # Breaking loop
       break
       ;;
     
   'n')  
       echo "Thank you for using this application"
      # Breaking loop
       break
       ;;
   *)
       # If user enters anything other than these options
       echo "Invalid entry..."
       break
       ;;
   esac
done

_________________________________________________________________________________________________

Sample Run:


Related Solutions

Miles to Kilometers ASSIGNMENT: Write a program to convert miles to kilometers. Put the entire program...
Miles to Kilometers ASSIGNMENT: Write a program to convert miles to kilometers. Put the entire program in a sentinel-controlled loop that runs until the user enters a negative number. Use both a pre-test sentinel-controlled loop and a post-test sentinel-controlled loop in the program. There are 1.6 kilometers in 1.0 mile. Store the value of 1.6 in a constant and use the constant in the calculations. There is 1 blank line after the descriptions, and 2 blanks lines between the pre-test...
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.
Using Bash script, 1. Print the multiplication table upto 10 rows. Ask the user to enter...
Using Bash script, 1. Print the multiplication table upto 10 rows. Ask the user to enter a number. say user enters 10, your output should be : [srivatss@athena shell]> ./mult.sh I will be printing the multiplication table Please enter a number 10 1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60 7 x 10 = 70 8 x 10...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
write a program bus management system? using: if else, for loop, do while loop, function, arrays,...
write a program bus management system? using: if else, for loop, do while loop, function, arrays, string, structure
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Write a denotational semantics for do-while loop
Write a denotational semantics for do-while loop
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT