Example File
oj Orange Juice
10 100
A half gallon of orange juice.
This is what I did and it worked
while read -r simpleName itemName; do
read currentQuantity maximumQuantity
read description
echo "Item Name: $itemName"
echo "Simple Name: $simpleName"
echo "Item Number: $itemNumber"
echo "Qty: $currentQuantity/$maximumQuantity"
echo "Description: $description"
done < $FILE
Note: Item number is prompted from the user prior to this and it determines which file to open.
This while loop correctly reads the file, stores the variables, and gives the desired output. The issue is, I used this same exact while loop two more times and it's not working. For some reason in the other two scenarios, simpleName and itemName are coming up blank, but the rest are working correctly.
Here's an example of where it is NOT working
#!/bin/bash
echo ""
read -p "Enter an item number: " itemNumber
echo ""
FILE="./data/${itemNumber}.item"
date=date "+%Y-%m-%d %H:%M:%S"
if test -f $FILE; then
if test -f ./data/queries.log; then
echo "DELETED: $date - $simpleName" >> ./data/queries.log
else
> ./data/queries.log
echo "DELETED: $date - $simpleName" >> ./data/queries.log
fi
while read -r simpleName itemName; do
read currentQuantity maximumQuantity
read description
done < $FILE
rm $FILE
echo "$simpleName was successfully deleted."
else
echo "ERROR: item not found."
fi
Here, the output comes out incorrectly as
was successfully deleted
So $simpleName is blank for some reason. I can't figure out why it works in the first instance but not this instance.
Note: Both instances are within separate script files.
I'm aware that there are simpler ways to do things, however, I'm restricted to very very basic commands, I am not allowed to use sed, awk, grep, find, pretty much just what you see below.
while
loops and note that they are in fact not the same in terms of where you use the data read by theread
utility. – Kusalananda Sep 19 '21 at 17:00