如何在 bash 中的 while 循环后再次读取工作

如何在 bash 中的 while 循环后再次读取工作
#!/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 " "

我已经编辑了这篇文章并放置了所有代码,以便有完整的上下文。bconsole是我正在调用的二进制可执行文件,该list media命令将数据转储到我正在使用 awk 逐行解析的标准输出。

问题是最后一条read语句没有接受用户的输入。我会收到提示,"[rescan]/exit: "并且我能够打字,但是当我按下它时,它只会再次要求输入而没有提示"[rescan]/exit: "。没有任何错误。我必须按 CTRL-C 退出脚本才能终止它,因为它卡在了(我假设)最后一条read语句上。我已经阅读了有关如何重定向标准输入的所有内容,并且在程序完成之前无法通过读取来利用它,但是我的while loop已完成,并且没有任何解决方案或修复似乎可以规避我的问题。

我试过了这个解决方案还有大约四个人,他们都没有让我更接近read接受用户输入的最后一条语句。

我假设它read仍然通过 while 循环进行重定向,但我认为一旦循环完成它就会释放。让它发挥作用的灵丹妙药是什么?

相关内容