Getting “command not found” error while comparing

2019-01-01 05:46发布

My whole Script is currently this:

#!/bin/sh   
clear;   
blanko="";   
# Dummy-Variablen
variable=Testvariable;   
if [[$variable == $blanko]];
then   
  echo "Nichts da!"   
else   
  echo $variable   
fi

and if i enter

TestSelect.sh

i get

/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found   
Testvariable

how can i fix this?

2条回答
伤终究还是伤i
2楼-- · 2019-01-01 06:36

This is problem:

if [[$variable == $blanko]];

Spaces are required inside square brackets, use it like this:

[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
查看更多
浪荡孟婆
3楼-- · 2019-01-01 06:37

On a related note, spaces are required around [ ] as well:

if [ "$variable" = "$blanko" ]; then
  # more code here
fi

Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.

Look at the following post to understand why we need spaces around [ ]:

Another related post that talks about other syntax elements that need spaces as well:

Finally, this post talks about the difference between [[ ]] and [ ]:


Related:

查看更多
登录 后发表回答