I am not getting 1 for the failure scenario for my below code. any help would be greatly appreciated. If the input is invalid i should get exit. But in the below scenario i am not getting the message exit o.
ksh -x client_check.ksh sun
+ + hostname
hn=us
+ [[ us= us ]]
+ [ ! -d ]
client_check.ksh[18]: test: argument expected
+ exit 0
pikd>shell_scripts> cat clientcheck.ksh
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
case $1 in
ban) client_home='/comps/banking'
;;
man) client_home='/comps/manufacturer'
;;
esac
fi
if [ ! -d ${client_home} ]; then
exit 1
else
exit 0
fi
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
case $1 in
ban) client_home='/comps/banking'
;;
man) client_home='/comps/manufacturer'
;;
esac
fi
if [ ! -d ${client_home} ]; then
exit 1
else
exit 0
fi
to:
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
case "$1" in
ban) client_home='/comps/banking'
;;
man) client_home='/comps/manufacturer'
;;
*) exit 1;;
esac
fi
Could you please change if [[ $hn = "us" ]] to if [[ "$hn" == "us" ]] and let me know if this helps.
EDIT: Thank you Don, I think Don has given perfect solution, please try that solution.
This isn't likely to help. (It will help if and only if hostname aborts or returns a host name containing whitespace characters.)
The problem shown in the trace is that the operand passed to the script ( sun ) is not a choice in the case statement. Therefore, the client_home variable is never set and the following test to determine if $client_home expands to the name of a directory gets a syntax error.
The change I suggested in post #2 in this thread short-circuits the problem by exiting immediately if client_home will not be set by the given operand.