Bash script conditionals simple error [duplicate] - bash

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?

This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"

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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash

Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh

If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Related

bash function to greet user differently based on number of arguments given [duplicate]

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?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

"command not found" error when using a variable [duplicate]

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?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Error running if statements in bash script [duplicate]

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?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

-bash: [: missing `]' error [duplicate]

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?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Bash Script if else not working [duplicate]

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?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
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 [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Resources