68.5k views
5 votes
One foot equals 12 inches. Design a function named feetToInches that accepts a

number of feet as an argument, and returns the number of inches in that many feet.
Use the function in a program that prompts the user to enter a number of feet and
then displays the number of inches in that many feet.

Full program shell-script in bash please. Thanks!

1 Answer

4 votes

#!/bin/bash

echo "Enter a number to be converted."

read number

feet = $(($number*12))

echo "feet conversion to inches ="$feet

Explanation -

1. #!/bin/bash - this tells the system that the following commands are to be executed by Bourne Shell. (Also known as the shebang, the # is called a hash, and the ! is called a bang).

2. echo "Enter a number to be converted." - this (i.e., 'echo') is used for displaying lines of text or string passed as an argument on the command line.

3. read number - read here is a command to read the input from the keyboard and assign it as the value of the variable number

4. feet = $(($number*12)) - here the '=' operator indicates that e are assigning the calculated value from right into the variable on the left.
And calculation for feet to inches is fairly simple -
1 feet = 12inches

n number of feet = n x 12 inches

5. echo "feet conversion to inches ="$feet - the echo will print the passed argument string onto the command line.
(We use the $ sign with a variable to determine if it is a variable we can use in an expression or as an argument to another method. )

User Romain Linsolas
by
8.6k points