0

echo $HOST on my laptop correctly shows Nbook as the hostname. But the simple shell script below always executes the first echo command regardless of the hostname I supply in the if condition above it. It seems I am missing something simple. Any help is appreciated. Thanks!

#!/bin/sh

if [ $HOST="whatever!" ]; then
  echo "This is Nbook"        # always executes!
elif [ $HOST="PC" ]; then
  echo "This is PC"
else
  echo "Unknown host"
fi

EDIT: The modified code below, now with whitespaces around = in the if condition, still does not give the expected result. I should now get This is Nbook, but it executes the last echo command Unknown host instead. My understanding from the answers here is that "$HOST" = "Nbook" should be interpreted as three separate arguments to the test function, in which case = is the intended operator and should return true. Looks like I am again missing something.

#!/bin/sh

if [ "$HOST" = "Nbook" ]; then
  echo "This is Nbook"
elif [ "$HOST" = "PC" ]; then
  echo "This is PC"
else
  echo "Unknown host"
fi

UPDATE: Thanks to Charles Duffy's help in the comments below, the following code works (I replaced $HOST by $(hostname):

#!/bin/sh

host=$(hostname)
if [ "$host" = "Nbook" ]; then
  echo "This is Nbook"
elif [ "$host" = "PC" ]; then
  echo "This is PC"
else
  echo "Unknown host"
fi
Manojit
  • 611
  • 1
  • 8
  • 18
  • 1
    Now that you fixed the initial problem, use xtrace to see what's going on at runtime -- `sh -x yourscript` to log each command as it's run, so you can see what value for `HOST` is actually being used in practice. The "EDIT"ed code is definitely correct, so I suspect that the `HOST` value isn't what you think it is. – Charles Duffy Aug 01 '23 at 17:22
  • 1
    (did you maybe set `HOST` as a shell-local variable but not export it? Only variables exported to the environment are visible to subprocesses -- though note that it's good practice to keep variables you define yourself outside the all-caps namespace used for variables meaningful to the shell and other POSIX-defined tools; see relevant standard at https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html). – Charles Duffy Aug 01 '23 at 17:23
  • 1
    ...if your parent shell is bash, you might also think about using `declare -p HOST` to print the variable's current definition before starting the script. If it says `declare -- HOST='Nbook'` that tells you it's not exported; if it says, say, `declare -x HOST=$'Nbook\r'` that tells you it _is_ exported, but was read from a DOS/Windows file instead of a native UNIX file and so has a stray carriage return on the end. – Charles Duffy Aug 01 '23 at 17:29
  • Interesting! I use zsh (on linux), never needed to export `HOST`, set it on all my machines the first time I install linux on them (and edit `/etc/hosts` on very rare occasions). Running `env` on terminal shows a long list of all environmental variables, `HOST` is not among them. – Manojit Aug 01 '23 at 18:21
  • `sh -x script` shows `+ [ = Nbook ]; + [ = PC ]; + echo Unknown host; Unknown host` – Manojit Aug 01 '23 at 18:22
  • 1
    There you are, now you know `HOST` doesn't exist as an environment variable. Zsh doesn't follow POSIX standards (which is why I refuse to use it), but even if it did it would be allowed to synthesize its own builtin variables in that all-caps reserved namespace. (As it is, zsh ignores the standard and also has special names _outside_ the reserved namespace as well). – Charles Duffy Aug 01 '23 at 18:28
  • 1
    Think about putting something like `[ "$HOST" ] || HOST=$(hostname)` in your script. – Charles Duffy Aug 01 '23 at 18:30
  • And running `declare -p HOST` returns `typeset HOST=Nbook`. – Manojit Aug 01 '23 at 18:33
  • 1
    BTW, think about `(exec -a sh bash -x yourscript)` to get your script run by bash in POSIX mode for better quality xtrace output; whichever sh you have doesn't quote in its trace logs, making them ambiguous. – Charles Duffy Aug 01 '23 at 18:33
  • Ah ha! Your 2nd last comment did the trick: I replaced `$HOST` by `$(hostname)` and voila! Thanks a bunch @CharlesDuffy for all your very insightful feedback! – Manojit Aug 01 '23 at 18:40
  • 1
    Note that running `$(hostname)` more than once is slower than running the `hostname` command just once and storing the result, so you probably don't want to repeat it in each `if` command. – Charles Duffy Aug 01 '23 at 19:26

0 Answers0