1

I am using AIX 6.1. Below code is working fine for me.

if [ "$HOSTNAME" = "host1" ]; then
  echo "This is host1"
elif [ "$HOSTNAME" = "host2" ]; then
  echo "This is host2"
else
  echo "This is another host"
fi

But when I use Shebang in my code, always the last else part is getting executed even if the first/second if conditions are true.

#!/bin/sh
if [ "$HOSTNAME" = "host1" ]; then
  echo "This is host1"
elif [ "$HOSTNAME" = "host2" ]; then
  echo "This is host2"
else
  echo "This is another host"
fi

How can I get the same output as in first code snippet but with using shebang.

P.S. : I can't write the script without using shebang due to some constraints.

Rohit
  • 169
  • 7
  • 17
  • Which shell is your login shell? Is it the same one as /bin/sh ? $> env | grep SHELL Will tell you your shell if you are running a bash variant – Spangen Oct 16 '17 at 13:10
  • env | grep SHELL gives SHELL=/usr/bin/ksh And my bad, there is a space inside if condition. Edited the question – Rohit Oct 16 '17 at 13:17
  • When using ksh, this page suggests you need [[ ]] https://www.cyberciti.biz/faq/ksh-if-command-examples/amp/ – Spangen Oct 16 '17 at 13:24
  • Tried, but still goes to last else. Not understanding how does it work when I don't use shebang line. – Rohit Oct 16 '17 at 13:29
  • When you don't use shebang, the currently running shell is used to evaluate the script. In your case ksh. Using the shebang tells your ksh shell that a different program is to be used to execute the contents, in this case /bin/sh. Anyway, the question has been closed now. Good luck :-) – Spangen Oct 16 '17 at 13:35
  • 1
    But its not duplicate question of the spaces before and after [] – Rohit Oct 16 '17 at 13:37
  • `HOSTNAME` is defined in bash, but /bin/sh isn't bash. Add this line into your script: `HOSTNAME="$(hostname)"` – Lorinczy Zsigmond Oct 16 '17 at 13:50
  • @rohit Maybe if you edited the question and title to reflect the two different shells, and asked chepner they might reopen it for you? – Spangen Oct 16 '17 at 14:03
  • @LorinczyZsigmond It worked like charm... Thank you :) – Rohit Oct 16 '17 at 14:17
  • A a style point, and one which would immediately expose error, write this as `case ${HOSTNAME?HOSTNAME not defined} in host1) echo "this is host1";; host2) echo "this is host2";; *) echo another host;; esac` – William Pursell Oct 16 '17 at 16:15
  • Not directly related to the question -- just extra info. AIX ships ksh, ksh93, and bsh. "sh" is a link to one of those (I'm recalling this from memory so please verify). bsh is the old Bourne shell. But ksh called as "sh" has some Posix requirements (again... IIRC). – pedz Oct 17 '17 at 01:06

2 Answers2

0

As Lorinczy Zsigmond suggested, added HOSTNAME="$(hostname)" at the beginning of the script and it worked.

Rohit
  • 169
  • 7
  • 17
0

Apparently, different shells are being used in the two scenarios (some shells automatically create certain variables).

A kernel execing a script without a shebang should behave the same as if the script had the shebang #!/bin/sh, however, it appears that some shells, namely bash, yash, and ksh (possibly others) are cheating by interpreting the script directly in a forked-child instead of execing it.

id_shell:

if false; then :
elif [ -n "${BASH_VERSION:-}" ]; then echo bash
elif [ -n "${KSH_VERSION:-}" ]; then echo ksh
elif [ -n "${POSH_VERSION:-}" ]; then echo posh
elif [ -n "${YASH_VERSION:-}" ]; then echo yash
elif [ -n "${ZSH_VERSION:-}" ]; then echo zsh
else
    echo sh;
fi

Test what shell is used to exec a shebang-less child script:

$ for sh in bash dash posh pdksh sh yash zsh ksh; do echo -n $sh: ; $sh -c './idself'; done

bash:bash
dash:sh
posh:sh
pdksh:sh
sh:sh
yash:yash
zsh:sh
ksh:ksh

To fix your particular issue, you could define HOSTNAME if it's not already defined:

: "${HOSTNAME:="$(hostname)"}"
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142