0

I created a simple script in bash language to do cleaning of the recycle bin in Ubuntu.

here is the script "recycle.sh"

#!/bin/bash
PUBLIC_PATH="/home/fileserv/.recycle/Public"
##### Clean public recycle folder
if [ !$(ls $PUBLIC_PATH | wc -l) -eq 0 ]; then
     /bin/rm -R $PUBLIC_PATH/*
fi

But when I run # ./recycle.sh I encounter thise error:

./recycle.sh: line 4: [: !1: integer expression expected

Can somebody explain why? Any solutions to make it work?

  • 1
    !1 is not an integer, put a space in there, or better yet just use -ne for not equal to. – 123 Jul 16 '15 at 10:38
  • 1
    The whole test condition is unnecessary here. Just remove all files - if there are no files in the directory nothing will be removed. – jimmij Jul 16 '15 at 12:49
1

Try this,

#!/bin/bash
PUBLIC_PATH="/home/fileserv/.recycle/Public"
##### Clean public recycle folder
if [[ !$(ls $PUBLIC_PATH | wc -l) -eq 0 ]]; then
    /bin/rm -R $PUBLIC_PATH/*
fi
  • Thanks, it works ! (idk, why adding a symbol '[ ]' make it works) – AdhiDazz Jul 18 '15 at 5:30
  • There are some special primitives that [[ is defined to have, but [ may be lacking(depending on the implementation), includes negation ! or checking a file is older or newer than the other file -ot or -nt etc etc.. Again, it's a big topic. This should be a different question. – Thushi Jul 21 '15 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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