1

I have a simple command for identify when file is empty but not works

 MN=$(echo "$(cat   empty)") ;

if [ MN == "" ]; then echo "This file is empty"; else echo "This file has been edited. You'll need to do it manually."; fi

whats I doing wrong

Please help me

zzero
  • 154
  • 1
  • 7
  • `[ MN == "" ]` is always going to be false (even ignoring that `==` isn't guaranteed to work: the only POSIX string comparison operator is `=`), because the string `"MN"` and the string `""` are never the same. If you meant `[ "$MN" = "" ]`, that's a different operation. :) – Charles Duffy May 18 '17 at 20:51
  • ...and btw, you could also write that as `[ -z "$MN" ]`. Note the quotes -- they're not optional if behavior needs to be robust. And as an aside -- all-caps variable names are specified for names with meaning to the OS or shell, whereas lowercase names are guaranteed not to conflict with variables having meaning to the shell or built-in utilities; see fourth paragraph of http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html – Charles Duffy May 18 '17 at 20:52
  • great ;very thankyou this help me for solve this – zzero May 18 '17 at 20:52

1 Answers1

2

test -s tests whether a file exists and is nonempty.

if test -e empty && ! test -s empty; then
  echo "This file exists but is empty"
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441