0
#!/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?

5
  • 3
    Please include what you mean by "isn't working". Also, what is bconsole? It doesn't seem to be a standard tool. Commented Dec 27, 2020 at 0:24
  • I've edited the question to be more concise. Commented Dec 27, 2020 at 4:39
  • You post is still not verbose enough, what errors do you face ? what output are you getting on the screen ? are you sure it is related with the read and not how the while loop is programmed ? Commented Dec 27, 2020 at 10:04
  • @JustKhaithang edited again for more verbosity. I really don't know. I tried moving the while loop around, it didn't help. I have a strange feeling it has something to do with the stdbuf -oL echo "list media" | bconsole | { .... } section and placement or something. Commented Dec 27, 2020 at 14:34
  • The pipe is using stdin of the process in { ... }, this is why the user input is not detected by the read. This is not the way bash works. To answer the question, it would be a complete refactoring of the script. Commented Dec 28, 2020 at 14:17

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.