I want to delete multiple images having resolution less then 228x228. For that, I wrote this shell script:
#!/bin/bash
for i in $( ls ); do
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
then
rm $i
fi
done
For some reasons, I got this output when I run it:
./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...
Could you please tell me what's wrong in this script and how to fix it.
Thank you.
EDIT: Even after I added spaces after the brackets, I still got an error. It was due to the usage of <
instead of -lt
and has been fixed. Now there is no error.
Some issues here: Firstly, the expression in a
[…]
test needs spaces around it (pitfall #10), and secondly the comparison<
doesn't work with[…]
tests (pitfall #7). You either need-lt
(less than) or use[[…]]
instead, which is a bashism. Also, thefor
loop should be replaced (pitfall #1).So:
You may also want to avoid calling
identify
twice to get the two dimensions (pitfall #58) but call it just once instead and let it print a string ready to be used as variable assignments in shell syntax.If we write
it will print something like
width=50 heigth=250
. When weeval
uate that string then we have set two variables with just one call and the condition can be written as:See also: common bash pitfalls.
Instead of a loop, I'd use
find
with-exec
and-delete
:This will also print the files that get deleted, you can remove
-print
if you don't want that.Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called
shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is inuniverse
for current stable.This is the output for your script
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.