In: Computer Science
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.
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: