0

I have this bash code:

#!/bin/bash
ls "$SOURCE" > cplist.txt
cat cplist.txt | while read FILE; do
    echo "Would you like to copy $FILE? y/n"
    read ANSWER
    if [ "$ANSWER" = "y" ]; then
        cp -v "$FILE" ./Destination;
    else
        echo "File NOT coppied"
    fi
done

The idea is to make a list of all files in a folder, then ask the user each file they want to copy to the destination folder. Currently this script wont allow user input after: "would you like to copy $file? y/n". I cant work out why as I'm very new to this kind of thing. Any ideas? Thanks in advance!

2 Answers 2

1

You've run into problems as you're using two separate calls to read but you're making things more complicated than they need to be. Try something like this:

#!/bin/bash

for file in "$source"/*; do
    echo "Would you like to copy $file? y/n"
    read -r answer
    if [ "$answer" = "y" ]; then
        cp -v "$file" ./Destination;
    else
        echo "File NOT copied"
    fi
done

Instead of parsing ls (which is generally considered a cardinal sin), this uses the shell's built-in glob expansion to match everything in the directory $source. I have added the -r switch to read, which is a good idea in general. I have also made your variable names lowercase. Uppercase variables should be reserved for built-in variables such as $PATH, which you certainly don't want to ever accidentally overwrite.

Sign up to request clarification or add additional context in comments.

Comments

0

You make the cplist.txt to the remaining part's standard input where the read tries to read from.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.