#!/bin/bash
echo "Export Tape(s):"
echo "---------------"
stdbuf -oL echo "list media" | bconsole | {
while IFS= read -r line
do
line=$(echo ${line^^})
if [[ "$line" == *"POOL"* ]] && [[ "$line" != *"DEFAULT"* ]] && [[ "$line" != *"FILE"* ]] && [[ "$line" != *"SCRATCH"* ]] && [[ "$line" != *"DUMMY"* ]]; then
echo " "
echo "$line" | awk '{print "["$2"]"}'
fi
if [[ "$line" == *"FULL"* ]]; then
inChanger=$(echo "$line" | awk '{print $20}')
if [[ "$inChanger" == 1 ]]; then
expiresIn=$(echo "$line" | awk '{print $31}')
if [[ "$expiresIn" != 0 ]]; then
echo "$line" | awk '{print " Date: "$28" Barcode: "$4}'
((tapesToExport++))
fi
else
newElement1=$(echo "$line" | awk '{print $4}')
if [[ $newElement1 != VOL* ]] && [[ $expiresIn = 0 ]]; then
newElement2=$(echo "$line" | awk '{print $28}')
tapeBarcode+=( $newElement1 )
tapeDate+=( $(date -d "$newElement2" +"%s") )
fi
fi
fi
done
IFS=$'\n' sorted=($(sort <<<"${tapeDate[*]}"))
unset IFS
sorted=( "${sorted[@]:0:$tapesToExport}" )
count=-1
for (( i=0; i<"${#tapeDate[@]}"; i++ )); do
(( count++ ))
if [[ "${sorted[$i]}" = "${tapeDate[$count]}" ]]; then
arrayOfRequiredIndexes[$i]=$count
else
(( i-- ))
fi
if [[ "$i" = "$((tapesToExport-1))" ]]; then
break
fi
done
if [[ "$tapesToExport" > 0 ]]; then
echo " "
echo -e "\e[1;37m╒═══════════════════════════════════════════════════════════════╕\e[0m"
echo -e "\e[1;37m│ Populate the TAPES.XLS file with the tapes above. │\e[0m"
echo -e "\e[1;37m│ Print EXCEL file and attach to each tape. │\e[0m"
echo -e "\e[1;37m│ │\e[0m"
echo -e "\e[1;37m│ Replace the "$tapesToExport" tape(s) that you'll export above │\e[0m"
echo -e "\e[1;37m│ with the "$tapesToExport" recommended below... │\e[0m"
echo -e "\e[1;37m╘═══════════════════════════════════════════════════════════════╛\e[0m"
echo " "
echo "Recommended Tapes to Import:"
echo "----------------------------"
for i in ${arrayOfRequiredIndexes[@]}; do
echo "Date: "$(date '+%Y-%m-%d' -d @${tapeDate[$i]})" Barcode: "${tapeBarcode[$i]}
done
else
echo " "
echo -e "\e[1;37mThere are no tapes to export.\e[0m"
echo " "
exit 1
fi
}
if [[ $? -eq 1 ]]; then
exit 0
fi
echo " "
echo "When you are finished importing the tape(s),"
echo "Type 'rescan' to issue a command for the tape"
echo "library to rescan. Or you can type 'exit' to"
echo "terminate the program."
while [[ "${userResponse}" != "exit" ]]; do
read -p "[rescan]/exit: " userResponse
if [[ "$userResponse" == "rescan" ]] || [[ -z "$userResponse" ]]; then
echo "update slots storage=TL2000-01 drive=0"
fi
done
echo " "
echo -e "\e[2;37mDone!\e[0m"
echo " "
I've edited the post and put all the code so that there's full context. bconsole is a binary executable that I'm calling and the list media command does a dump of data to the stdout that I'm parsing with awk line-by-line.
The problem is that the very last read statement isn't taking input from the user. I will get prompted with "[rescan]/exit: " and I have the ability to type, but when I press it just asks for input yet again without the "[rescan]/exit: " prompt. There are no errors whatsoever. I have to CTRL-C out of the script to terminate it because it gets stuck (I'm assuming) on the last read statement. I've read all about how stdin is redirected and can't be leveraged by read until the program is complete, however my while loop is complete and none of the solutions or fixes seem to circumvent my problem.
I've tried this solution and about four others and neither of them are getting me closer to getting the last read statement accepting input from a user.
I'm under the assumption that read is somehow still redirected with the while loop, but I thought that it would release once the loop is done. What would the silver bullet be to get this to work?
bconsole? It doesn't seem to be a standard tool.stdbuf -oL echo "list media" | bconsole | { .... }section and placement or something.{ ... }, this is why the user input is not detected by theread. This is not the way bash works. To answer the question, it would be a complete refactoring of the script.