Help with ksh Shell Script

My goal is to create a script that will check if in a test or production environment. I wrote this script to check $host variable to check which server I'm on but this script does not work.

if [ "$host" == "abc_test" ]
then
       BASE=/home/fmtest; export BASE
else
       BASE=/home/fmprod; export BASE
fi
 
echo $BASE

No matter which server I run this on (Test or Production) it echo's "/home/fmtest"

if [ $(hostname) == "abc_test" ]
then  
  BASE=/home/fmtest; export BASE
else
  BASE=/home/fmprod; export BASE
fi

echo $BASE

--ahamed

An if this is actually "ksh", the "==" is invalid: Anyway we always put quotes round a string comparison.

if [ "$(hostname)" = "abc_test" ]
then  
  BASE=/home/fmtest; export BASE
else
  BASE=/home/fmprod; export BASE
fi

echo $BASE

Worth checking what the output from the hostname command looks like because it should be the full qualified hostname and may be different from uname -n .

1 Like

Thank you so much, this is now working as designed!:slight_smile:

It is not necessary to uses quotes in ksh93 unless you have white spaces in your string. This works as intended:

if [[ $(hostname) = IISC08 ]]
then
    echo "IISC08"
fi

@fpmurphy
Agreed. It's also not necessary in ksh88. If you omit the quotes you do get a syntax error if any of the variables are empty.
However I'm on a one-man mission to try to get the populus to put quotes round strings because this second only to misuse of "for" in the common scripting errors posted on this board.