-1

I have a bunch of 7-zip files under a directory and I would like to extract all of them and then delete successful ones in bash. I wrote this: for f in *.7z; do if 7z e "$f" then echo "error $f" else rm "$f" fi; done

and bash prompts: -bash: syntax error near unexpected token done'`. How to fix this statement?

fhcat
  • 971
  • 2
  • 9
  • 28
  • `bash does not like it` Sure, he may not like it, but does `bash` execute the commands? What happens when you type them in terminal? Do you get `bash: I do not like that` mesage? `How to fix this statement?` What is there to fix? Is there something broken? What exactly is broken? (Do you really ask about emotional state of bash towards your code?) Your code, as it is now, is posted as a single line. Are you typing your command as a single line in your terminal? Or are there some newlines between? – KamilCuk Jun 27 '20 at 18:36
  • Figure out a way to specify that extraction failed due to the file being actually corrupt, or the process itself not working. I personally would not automate rm without keeping a backup of the files in question; too many bad experiences with that. – petrus4 Jun 28 '20 at 00:07

1 Answers1

0

Your one-liner is missing a few semi-colons. Try this

for f in *.7z; do if 7z e "$f"; then echo "error $f"; else rm "$f"; fi; done
LeadingEdger
  • 604
  • 4
  • 7