Search code examples
bashrm

Deleting files and prompting user for his decision


printf "$(find "./RIT/"${neki_array["letnik"]}"" -name "*" )\n">> tmpfile
while IFS='' read -r line || [[ -n "$line" ]] ; do
      echo "Do you want to delete "$line"? y|Y or n|N" 
      read anwser
      if [ $anwser="y" ] || [ $anwser="Y" ] ; then
         rm -rf "$line"
      fi
done <tmpfile
rm -f tmpfile

That is my code. What I am trying to do is save every sub folder or text file to my temporary file called tmpfile. When I'm done with that i read this file line by line and ask user if he wants to delete it. The problem is that it skips user input and the if statement and deletes all of the folders.


Solution

  •  printf "$(find "./RIT/"${neki_array["letnik"]}"" -name "*" )\n">> tmpfile
            while IFS='' read -r line || [[ -n "$line" ]] ; do
                echo "Do you want to delete "$line"? y|Y or n|N" 
                read anwser < /dev/tty
                case $anwser in
                    y|Y)
                        rm -rf "$line";;
                    *)
                        echo "You choose not to delete"
                esac
            done <tmpfile
            rm -f tmpfile
    

    I used the < /dev/tty and put a switch case insted of if and it worked.