Shell scirpt error

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

Try changing:

#!/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
1 Like

Hello Arun,

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.

Thanks,
R. Singh

1 Like

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.

2 Likes

thanks for your help ravi/don.

I am not sure why the $1 variable is not assigned to the below code.

  case $1 in
      ban)   client_home='/comps/banking'
             ;;
      man)     client_home='/comps/manfucture'
             ;;
       *)     client_home='/comps/$1'
             ;;

ksh -x client_check.ksh jkknk
+ client_home=/comps/$1

That's because of the single quotes, which prohibit shell's variable expansion. Use double quotes instead.

1 Like

thanks for your help rudic. it works.