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.
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...
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 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
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...
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
Write a while loop to ask the user to type number of hours(double) they work per...
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 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT