Nicole
Nicole

Reputation: 1285

if statement to check $HOSTNAME in shell script

So I want to set some paths differently depending on the host, but unfortunately it's not working. Here is my script:

if [$HOSTNAME == "foo"]; then
    echo "success"
else
    echo "failure"
fi

This is what happens:

-bash: [foo: command not found
failure

I know for certain that $HOSTNAME is foo, so I'm not sure what the problem is. I am pretty new to bash though. Any help would be appreciated! Thanks!

Upvotes: 51

Views: 100923

Answers (4)

Bash solution:

if [[ $HOSTNAME == 'foo' ]]; then
  echo 'yep this is foo'
else
  echo 'not foo'
fi

In regard to the response from @alper :

You can combine your logical OR (||) like this:

if [[ $(hostname) == "home" || $(hostname) == "home2" ]]; then
  echo "True"
fi

It's fine to do it the way you did, but I thought you might want to know you can save some keystrokes. One other critique is that using a subshell $() to execute the hostname command is slower than comparing against $HOSTNAME. There are valid reasons to do this in some environments, but the builtin environment variable $HOSTNAME should suffice for most simple use cases.

In response to @Jens: Bash supports the POSIX-compliant string-equality comparison operator (=), but has its own string-equality comparison operator (==) to differentiate it from the POSIX-compliant operator. This mirrors how bash's built-in test construct ([[ ]]) is doubled to differentiate it from the POSIX-compliant test contract ([ ]).

Calling bash a 'buggy shell' because it 'accepts' its own string-equality construct is a complete misrepresentation.

Saying that '==' is accepted by 'buggy shells in an attempt to ease programmers with less error messages' is totally false, not to mention bad grammar. You meant 'fewer' error messages.

Saying that '== is a bug' and 'some shells only accept it because of widespread ignorance' is completely wrong.

Solaris is being replaced with Linux more and more with each passing year in corporate environments. The majority of *nix servers run some flavor of Linux. So POSIX compliance in all shell scripts is no longer a hard requirement, and is increasingly irrelevant for a lot of environments.

Bash won the shell wars, because Linux won the *nix wars.

Upvotes: 0

Danstahr
Danstahr

Reputation: 4319

You're missing a space after the opening bracket. It's also good "not to parse" the variable content using double quotes:

if [ "$HOSTNAME" = "foo" ]; then 
    ...

Upvotes: 4

alper
alper

Reputation: 3410

#!/bin/bash

if [[ $(hostname) == "home" ]] || [[ $(hostname) == "home2" ]]; then
    echo "True"
fi

Upvotes: 2

Jens
Jens

Reputation: 72667

The POSIX and portable way to compare strings in the shell is

if [ "$HOSTNAME" = foo ]; then
    printf '%s\n' "on the right host"
else
    printf '%s\n' "uh-oh, not on foo"
fi

A case statement may be more flexible, though:

case $HOSTNAME in
  (foo) echo "Woohoo, we're on foo!";;
  (bar) echo "Oops, bar? Are you kidding?";;
  (*)   echo "How did I get in the middle of nowhere?";;
esac

Upvotes: 83

Related Questions