209k views
4 votes
A data file "parttolerance.dat" stores on one line, a part number, and the minimum and maximum values for the valid range that the part could weigh. Write a script "parttol" that will read these values from the file, prompt the user for a weight, and print whether or not that weight is within range. For example, IF the file stores the following:>> type parttolerance.dat123 44.205 44.287Here might be examples of executing the script:>> parttolEnter the part weight: 44.33The part 123 is not in range>> parttolEnter the part weight: 44.25The part 123 is within range

1 Answer

5 votes

Answer:

First, create the parttolerance.dat file with the line in the question. This can easily be done by using the echo command from the command line:

echo 123 44.205 44.287 >> parttolerance.dat

Then write a simple script file that prompts the user for the weight and then compares that input to the values within the file created above. If the value of the weight is within the range, then print the value is within range. Otherwise print it is not within range.

Step-by-step explanation:

// create the parttolerance.dat file

echo 123 44.205 44.287 >> parttolerance.dat

Shell script

#!/bin/bash echo

"Enter the part weight: "

read input_weight

IFS=' ' while IFS=' '

read -r col1 col2 col3 do

part_number="$col1"

file_weight_lower="$col2"

file_weight_upper="$col3"

done <parttolerance.dat

if (( $(bc <<< "$input_weight >= $file_weight_lower && $input_weight <= $file_weight_upper") > 0 )); then

echo "The part $part_number is within range"

else

echo "The part $part_number is not in range"

fi

run script:

./parttol.sh

User Kamyar Nazeri
by
4.3k points