Reputation: 696
I am working on a small shell script for backing up the sever logs to amazone. We have multiple servers running on production. So I wrote a script that dynamically detect the server and do the back up. But I am facing an issue while comparing two strings. I will give the code snippet below I wrote and the error.
test.sh
host="$(hostname)"
if [ "$host" == "server1-myapp.com" ]; then
function_1 $host
elif [ "$host" == "server2-myapp.com" ]; then
function_2 $host
elif [ "$host" == "server3-myapp.com" ]; then
function_3 $host
fi
function_1 () {
echo "host name is $1"
}
function_2 () {
echo "host name is $1"
}
function_3 () {
echo "host name is $1"
}
But when running test.sh as sh test.sh I am getting the following error.
test.sh: 2: [: server1-myapp.com: unexpected operator
test.sh: 4: [: server1-myapp.com: unexpected operator
test.sh: 6: [: server1-myapp.com: unexpected operator
I tried different ways to match two strings as one is a variable and other is a inline string, it is not matching strings properly can any one please help me, I am kind off stuck.
Upvotes: 0
Views: 113
Reputation: 8164
To compare string in shell with the [ ]
operator it's with =
and not ==
. This will works:
if [ "$host" = "server1-myapp.com" ]; then
Upvotes: 1