0
set temp = "sunny45"

if [ - z $temp ]; then echo "empty" ; else echo "not empty" ; fi

The above code is giving below error when i tried this code on unix terminal

if: Expression syntax.

Please suggest should i source any thing in file and what should ne the file name of this code present.

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
Anil
  • 1
  • 1

2 Answers2

1

Whipe spaces around equal sign and between - and z, then enclose variable in double-quotes:

temp="sunny45"

if [ -z "$temp" ]; then echo "empty" ; else echo "not empty" ; fi
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
1

You have multiple syntax errors. Probably try with http://shellcheck.net/ before asking for human help.

set will not set a regular variable. I'm guessing you want

temp="sunny45"

where you need to make sure there is no whitespace on either side of the equals sign.

The -z in the test similarly needs to be a single token, with no internal whitespace. You also need to quote the variable.

if [ -z "$temp" ]; then
tripleee
  • 175,061
  • 34
  • 275
  • 318