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

  • 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? – aghast Jan 28 '16 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 – user2874977 Jan 28 '16 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. – P.P Jan 28 '16 at 16:46
  • If you do switch your shebang to bash, use `read -p "delete? " text` -- and pick a better variable name. – glenn jackman Jan 28 '16 at 16:55

3 Answers3

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
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Try to use double brackets:

if [[ "$text" == "yes" ]]; then echo yes; fi
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

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

#!/bin/bash
gollum
  • 2,472
  • 1
  • 18
  • 21