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?

2
  • 1
    !1 is not an integer, put a space in there, or better yet just use -ne for not equal to.
    – 123
    Commented Jul 16, 2015 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
    Commented Jul 16, 2015 at 12:49

1 Answer 1

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
2
  • Thanks, it works ! (idk, why adding a symbol '[ ]' make it works)
    – AdhiDazz
    Commented Jul 18, 2015 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
    Commented Jul 21, 2015 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .