Operating System - HP-UX
1832252 Members
3016 Online
110041 Solutions
New Discussion

Check the directory exist

 
SOLVED
Go to solution
hangyu
Regular Advisor

Check the directory exist

I have the below script to check whether the directory is exist or not , I sure the remote_host directory /tmp/abcde is not exist , but it sill pop "connect OK" , even I check any directory in the remote_host , the result is also "connect OK" , could suggest what is wrong ? thx

rsh remote_host -n "ls -ld /tmp/abcde"
if [ $? != 0 ]
then
echo "connect OK"
else
echo " Connect Not OK !!"
fi
9 REPLIES 9
Vibhor Kumar Agarwal
Esteemed Contributor
Solution

Re: Check the directory exist

Shouldn't it be the other way round.

rsh remote_host -n "ls -ld /tmp/abcde"
if [ $? = 0 ]
then
echo "connect OK"
else
echo " Connect Not OK !!"
fi
Vibhor Kumar Agarwal
hangyu
Regular Advisor

Re: Check the directory exist

thx reply ,

I tried your method , there is error message but still pop "connect OK" , it is strange , could suggest what is wrong ? thx


ls: /tmp/abcde: No such file or directory
Connect OK!!
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Check the directory exist

I think you will have to break up your script.

Your exit code is coming 0 because its that of "rsh" and not "ls".
Your rsh is running fine and that's why its giving the exit code of 0.

Try it and let us know.
Vibhor Kumar Agarwal
Manoj_36
Advisor

Re: Check the directory exist

Hi Hangyu,
As your scripts shows is that if the command on remote host fails then it will echo a message "connect ok" and if successed then it will say "connect not ok'. So you have just to delete the "!" from your script and it will work. Just write as

remsh -n "ll -ld /tmp/abcde"
if [ $? = 0]
then
echo "Connect OK"
else
echo "Connect Not OK"
fi

If you run this script, you will get the line showing your directory and connect ok in case if it exists. And if /tmp/abcde not exists you will get /tmp/abcde not found along with the message connect not ok.
Try this and hope it will work for you.

Thanks.
Manoj_36
Advisor

Re: Check the directory exist

Hi Hangyu,
Hi hangyu in my last reply by mistake I have written in the result out put connect not ok but it is connect ok. You are getting connect ok each time because your remsh is always running successfully and it is giving 0 value for $? each time so it is showing connect ok. You can write your script as below
remsh host_name -n "ll -ld /tmp/abcde" | grep "not found"
if [ $? = 0 ]
then
echo "directory not exists"
else
echo "directory exists"
fi

Here actually you have to check for the result of command 'remsh' not the exit status of command 'remsh'.
Try this and I hope it will work now.
Thanks
Eknath
Trusted Contributor

Re: Check the directory exist

Hi Hangyu,

Dont you feel that $? is showing you the exit status of rsh command rather than you ls -ld command.

I think manoj`s script should work.

Cheers!!!
eknath

Ralph Grothe
Honored Contributor

Re: Check the directory exist

Hi Hanguy,

may I quote from man remsh:

Note that the return value of remsh bears no
relation to the return value of the remote command.


So it's better not to rely on the return code of the remsh command since it only tells you something about its socket state.

Why don't you do something like this?

rc=$(remsh remote_host '[ -f /some/file/there ] && echo ok')
if [[ $rc = ok ]]; then
echo "remote file present"
fi
Madness, thy name is system administration
A. Clay Stephenson
Acclaimed Contributor

Re: Check the directory exist

What you should do is echo ${?} of the last command executeded on the remote host. ${?} on the local host only tells you if remsh itself launched okay.

This should work:

STAT=$(remsh remote_host "test -d /xxx/mydir; echo \${?}")
if [[ ${STAT} -eq 0 ]]
then
echo "Directory exists"
else
echo "Directory does not exist; status ${STAT}"
fi

Note the \ before ${?}.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Check the directory exist

This should work:

STAT=$(remsh remote_host "test -d /xxx/mydir; echo \${?}")
if [[ ${STAT} -eq 0 ]]
then
echo "Directory exists"
else
echo "Directory does not exist; status ${STAT}"
fi

Note the \${?}.
If it ain't broke, I can fix that.