0

I have a problem with bash script that should go through every line in ready.txt file which has stored paths to files and then do something with them like in this case rm -f

ready.txt contents:

'/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI2.pdf'
'/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI4.pdf'
'/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI6.pdf'

My loop that should delete thoese files but doesn't do that.

while read -r line;
do
    rm -f $line
done < ready.txt

I tested it manually:

export line='/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI2.pdf'

rm -f $line

or:

rm -f '/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI2.pdf'

Both of these I tested works as intended, but loop doesn't want to.

I a newbie in bash scripts. Am I missing something?

3
  • Does ready.txt contain quotes ' themselves?
    – KamilCuk
    Commented Mar 22, 2021 at 21:38
  • Yes it does contain ' Commented Mar 22, 2021 at 21:44
  • @spyx33 That's why it doesn't work. You need to remove from passing to rm (or you could from the read.txt itself, too). In any case, use double quotes for the variable line.
    – P.P
    Commented Mar 22, 2021 at 21:47

2 Answers 2

0

It doesn't work because the filenames contain single quotes in your input file ready.txt (unless the actual filenames do contain quotes, which I doubt) as rm doesn't find such files.

You need to remove them:

while read -r line;
do
    rm -f "${line//\'}"
done < ready.txt

Also, see When to wrap quotes around a shell variable?

1
  • Yup this is a correct answer now it does work with ' quotes! Thanks a lot! Commented Mar 22, 2021 at 21:56
0

Am I missing something?

Checking your scripts with http://shellcheck.net before posting here.

Assuming the input file contiains:

/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI2.pdf
/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI4.pdf
/home/kamil/TEST/FOLDER ZE SPACJAMI/PLIK ZE SPACJAMI6.pdf

Quote every variable expansion to prevent word splitting:

rm -f "$line"

Most probably rm -f $line "works" in your shell because you are using zsh, not bash, while in script you use bash or sh.

Also read -r line will remove leading and trailing spaces. Use:

while IFS= read -r line

For more information, research word splitting, when and how to use quoting in shell, how double quotes differ from single quotes. Also see https://mywiki.wooledge.org/BashFAQ/001 .

To execute a command for each line of a file, use xargs, typically:

xargs -d '\n' rm < ready.txt
2
  • Thanks for the shellcheck.net tool! I newer heard of it and I already know I will be using it a lot :D Commented Mar 22, 2021 at 21:51
  • I really complicated it :D I have 2 files one with ' quotes and one without them. Solution you posted works for the one without ' quotes so it's a lot simpler than my previous. I couldn't get it to work before because spaces were messing it up but now it works great with while IFS= read -r line Commented Mar 22, 2021 at 22:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.