Hi I'm new to bash scripting. Just wrote this simple program but it is throwing error.
#!/bin/bash
os=`uname -o`
echo $os
if ["$os"=="GNU/Linux"] ; then
echo "Linux"
else
echo "Windows"
fi
Using == or -eq for both cases I'm getting the following error and it is printing the else condn.
./ostype.sh: line 3: [GNU/Linux==GNU/Linux]: No such file or directory
Windows
Bash version : GNU bash, version 3.2.48(1)-release (x86_64-suse-linux-gnu)
When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.
You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!= ” is used to check inequality of the strings. You can partially compare the values of two strings also in bash.
The Bash logical (&&) operator is one of the most useful commands that can be used in multiple ways, like you can use in the conditional statement or execute multiple commands simultaneously.
try
if [ "$os" = "GNU/Linux" ]
note the spaces, and the single =
.
[
is actually a program, and the rest are arguments!
Use =
for string comparison. See: http://tldp.org/LDP/abs/html/comparison-ops.html
Also, there should be a space around the square brackets and the comparison operator, i.e.
if [ "$os" = "GNU/Linux" ]; then
^ ^ ^ ^ ^
| | | | |
\-\-----\-\-----------\-- (need spaces here)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With