0

I'm trying to write a shell script to delete 0kb files but the if statement keeps giving me errors, and i'm wondering if you can help me out

I have:

#!/bin/sh
EMPTY_FILE=$(find ${1-.} -size 0)
echo $EMPTY_FILE
echo "delete"
read text
if [ "$text" == "yes" ]; then echo yes; fi

error is

./deleteEmpty.sh: 6: [: yes: unexpected operator

Any help on whats wrong is useful! Thanks

4
  • This appears to work for me. That is, if I run that script, and type "yes", I get "yes" back. Is that not what you are expecting? Commented Jan 28, 2016 at 16:42
  • Hm.. it doesn't work for me. is there something i need to install/change for this to work? Edit: oh man, i used /sh instead of bash Commented Jan 28, 2016 at 16:45
  • 1
    You are the test command in which you should use = for string comparison instead of ==. If you are using bash, then == would be fine too. Commented Jan 28, 2016 at 16:46
  • If you do switch your shebang to bash, use read -p "delete? " text -- and pick a better variable name. Commented Jan 28, 2016 at 16:55

3 Answers 3

3

In plain sh the equality operator is =. == is a bashism. Either change your shebang line to #!/bin/bash, or change the test to:

if [ "$text" = "yes" ]; then echo yes; fi

If you use bash, I recommend using [[ instead of [.

#!/bin/bash

if [[ $text == yes ]]; then echo yes; fi
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use double brackets:

if [[ "$text" == "yes" ]]; then echo yes; fi

Comments

0

I suppose you want to use bash. In that case the first line of your script should be

#!/bin/bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.