2

I'm downloading files from a remote server. This part works fine but now I'm trying to compare the remote file size to the local file size.

If the two don't match then I want to prompt the user to enter yes or no

I've added the read command, but the script never pauses and asks the question. why ?

This is my test code

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

Can anyone advise what I've done wrong. thanks

Update.. I've intentionally set it so a local file will have the wrong size so I can test.. I get the error error... but not the prompt asking if they want to continue.. any ideas

Fixed Typo

Tom
  • 1,436
  • 24
  • 50

2 Answers2

2

You can use this (inspired by dank's answer):

read -p "Continue (y/n)?" CONT </dev/tty
Rene Knop
  • 1,788
  • 3
  • 15
  • 27
2

That's because the read inside the loop will also read from standard input, which was redirected from filelist. A standard way (in Bash) is to use another file descriptor for the redirection of 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
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104