Reputation: 15
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!
Upvotes: 0
Views: 325
Reputation: 13725
You make the cplist.txt to the remaining part's standard input where the read tries to read from.
Upvotes: 0
Reputation: 74595
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.
Upvotes: 1