比较文件文件,提示不要求输入

comparing file files, prompt not asking for input

我正在从远程服务器下载文件。这部分工作正常,但现在我正在尝试将远程文件大小与本地文件大小进行比较。

如果两者不匹配那么我想提示用户输入yes or no

我添加了读取命令,但脚本从不暂停并询问问题。为什么?

这是我的测试代码

while IFS=',' read -r downloadfiles; do


        case "$downloadfiles" in
        AA)
            filetoget="$downloadfiles.tar.gz"
            ;;
        BB)
            filetoget="$downloadfiles.zip"
            ;;                      
        esac


        sizeoffile=`curl -sI "http://server.com/$filetoget" | awk '/Content-Length/{sub("\r","",$NF); print $NF}'`
        curl -O http://server.com/$filetoget

        localsizeoffile=`stat --print="%s" $filetoget`

        if [ "$localsizeoffile" -ne "$sizeoffile" ]; then
            echo "error..."

            read -p "Continue (y/n)?" CONT
                if [ "$CONT" = "y" ]; then
                    echo "yaaa";
                else
                    echo "booo";
                fi
            fi

done < filelist

任何人都可以告诉我做错了什么。谢谢

更新.. 我故意将其设置为使本地文件的大小错误,以便我进行测试。我收到错误 error...,但没有提示询问他们是否要继续。任何想法

修正错别字

你可以使用这个(灵感来自 dank's answer):

read -p "Continue (y/n)?" CONT </dev/tty

那是因为循环内的 read 也会 从标准输入中读取,它是从 filelist 重定向而来的。一种标准方法(在 Bash 中)是使用另一个文件描述符来重定向 filelist:

# Read from file descriptor 10: see end of loop, 10 is the redirection of filelist
while IFS=, read -u 10 -r downloadfiles; do
    # ...
    if (( localsizeoffile != sizeoffile )); then

        echo "error..."

        # This will read from standard input
        read -p "Continue (y/n)?" cont
        if [[ $cont = y ]]; then
            echo "yaaa"
        else
            echo "booo"
        fi
    fi

# Redirect filelist to file descriptor 10
done 10< filelist