LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-17-2011, 10:41 PM   #1
sudhirav
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Rep: Reputation: 0
Shell script to check whether directory exists on remote server


Hi All,

I am new to scripting, would like to have a script that tests whether a directory exists on remote host & display the message accordingly. The remote hostname can be provided by means of file containing list of hostnames. Can use rsh for connecting to remote host.

I tried with couple of scripts by searching google but didn't get desired result. Please help me, below is my efforts, $file contains list of hostnames.

for i in $file
do
if ( [ `rsh $i test -d /tmp/.X*` ] || [ `rsh $i test -d /scratch/.X*` ] )
then
echo "directory exist"
else
exit 0;
fi
done

Thanks in advance, appreciate all the efforts.
 
Old 01-18-2011, 01:39 AM   #2
zjoske
LQ Newbie
 
Registered: Dec 2009
Posts: 18

Rep: Reputation: 2
You have a very nice test if your remote server is up....

The command [ `rsh $i test -d /tmp/.X*` ] will always yield "0" when you are able to access the remote server.. It will not refect the value of the test command's result.

You'd probably want something like

[ `rsh $i "[[ -d /tmp/.X* ]] && echo true"` == "true" ]

Here you do the test if your directory is on your remote server and you pass the keyword true.
On your local server you check for the keyword.


Please note that the test on the remote server is ksh and does not necesary work on your server. I do not want to supply you with a proper solution, I just want to point out where your mistake is.
 
1 members found this post helpful.
Old 01-18-2011, 01:44 AM   #3
cin_
Member
 
Registered: Dec 2010
Posts: 264

Rep: Reputation: 23
results

test() seems fine...
Quote:
if ( [ `rsh $i test -d /tmp/.X*` ] || [ `rsh $i test -d /scratch/.X*` ] )
...you can write your if statement without the ( )
What are the results you are getting and what are the desired results?

Last edited by cin_; 01-18-2011 at 01:51 AM. Reason: gramm'err
 
Old 01-18-2011, 05:07 AM   #4
sudhirav
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Hi zjoske,

Thanks for the help, ran the script as per your suggestion getting following error :

#!/usr/bin/bash
if [ `rsh us01term4 "[[ -d /tmp/testsav ]] && echo true"`=="true" ]
then
echo "directory exists"
else
echo "directory doesn't exist"
fi

~/lsf_scripts % ./t.sh
[[: Command not found.
directory exists


The directory /tmp/testsav doesn't exist but the output displayed is "directory exists"/






Quote:
Originally Posted by zjoske View Post
You have a very nice test if your remote server is up....

The command [ `rsh $i test -d /tmp/.X*` ] will always yield "0" when you are able to access the remote server.. It will not refect the value of the test command's result.

You'd probably want something like

[ `rsh $i "[[ -d /tmp/.X* ]] && echo true"` == "true" ]

Here you do the test if your directory is on your remote server and you pass the keyword true.
On your local server you check for the keyword.


Please note that the test on the remote server is ksh and does not necesary work on your server. I do not want to supply you with a proper solution, I just want to point out where your mistake is.
 
Old 01-18-2011, 05:19 AM   #5
sudhirav
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Hi

I would like to chek whether either /tmp/.X* or /scratch/.X* exists & if they do display message the directory exists accordingly.

Following is the script I run & output is as follows :

#!/usr/local/bin/bash
file=`~/lsf_scripts/bhosts_output`
for i in $file
do
if [[ `rsh $i test -d /tmp/.X*` ]] || [[ `rsh $i test -d /scratch/.X*` ]]
then
echo "directory exist"
else
echo "directory doesn't exit"
fi
done

#./p2.sh
/remote/ushome17/sav/lsf_scripts/bhosts_output: line 1: atto342: command not found
/remote/ushome17/sav/lsf_scripts/bhosts_output: line 2: atto343: command not found



For every hostname in the for loop for value of i an error command not found is displayed on the screen, nothing else. From the CLI I can do rsh or ping to the hosts atto342, atto343.

Quote:
Originally Posted by cin_ View Post
test() seems fine...

...you can write your if statement without the ( )
What are the results you are getting and what are the desired results?
 
Old 01-18-2011, 05:53 AM   #6
cin_
Member
 
Registered: Dec 2010
Posts: 264

Rep: Reputation: 23
try using Wrap [CODE]#[/CODE] tags for easy to read source

So for i in FILE it is going to go by each line in the file. If there is information, especially divided by a space ' ', then when bash reads it it will read the second piece as an argument or another command. So, I am thinking that is what is going on here: atto343: command not found.

Code:
...
if [[ `rsh "$i" test -d /tmp/.X*` ]] || [[ `rsh "$i" test -d /scratch/.X*` ]] 
...
... might just solve that problem, or expose another...

Can you...
Code:
# cat FILE
... so I can see how the file is set up.

Last edited by cin_; 01-18-2011 at 10:42 AM. Reason: gramm'err
 
Old 01-18-2011, 06:46 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If rsh behavies like ssh, then:
ssh user@host "[ -d $DIR ]" && echo "dir exists"
will work.

Code:
DIRS="Documents Document Downloads Download"
for dir in $DIRS; 
   do ssh elite "[ -d $dir ]" && echo "$dir exists" || echo "$dir doesn't exist"
done

Documents exists
Document doesn't exist
Downloads doesn't exist
Download exists
Your original thread tested different hosts for a directory. Not as easy for me to demonstrate with a real example.

You shouldn't be using rsh if your lan has a network connection. Ssh is a more secure replacement.
You can use:
eval $(ssh-agent)
ssh-add

to be able use public key authentication, and not have to enter the passphrase each time.

---
Be careful you understand when and where a variable is referenced.

This example will send the command verbatim to the server.
ssh user@host 'ls $HOME/Documents/'

This example will evaluate the variable $dir before executing the ssh command:
ssh user@host "ls $dir/*.pdf"

In my original example, the test "[ -d $dir ]" is made on the server after $dir is referenced on the client. The rest of the line runs on the client ( && echo "$dir exists" || echo "$dir doesn't exist" ).
 
Old 01-18-2011, 07:55 AM   #8
zjoske
LQ Newbie
 
Registered: Dec 2009
Posts: 18

Rep: Reputation: 2
Hi Sudhirav,

If it does not work it might be the shell, try replacing "[[" with "[".

I am working with the kshell, it works both for me (this is just the test on the remote server):

==> with two brackets
zrh1lw01[teuniss] rsh zrh1l013 "[[ -f caramba ]] || echo false"
false
zrh1lw01[teuniss] rsh zrh1l013 "[[ -f superkaramba-0.39.tar.gz ]] || echo false"
zrh1lw01[teuniss]

==> with one bracket
zrh1lw01[teuniss] rsh zrh1l013 "[ -f superkaramba-0.39.tar.gz ] || echo false"
zrh1lw01[teuniss] rsh zrh1l013 "[ -f caramba ] || echo false"
false

If the test passes an retuns false in this case, you can process it in your script with an additional if statement.



Quote:
Originally Posted by sudhirav View Post
Hi zjoske,

Thanks for the help, ran the script as per your suggestion getting following error :

#!/usr/bin/bash
if [ `rsh us01term4 "[[ -d /tmp/testsav ]] && echo true"`=="true" ]
then
echo "directory exists"
else
echo "directory doesn't exist"
fi

~/lsf_scripts % ./t.sh
[[: Command not found.
directory exists


The directory /tmp/testsav doesn't exist but the output displayed is "directory exists"/
 
Old 01-18-2011, 08:10 AM   #9
zjoske
LQ Newbie
 
Registered: Dec 2009
Posts: 18

Rep: Reputation: 2
Sudhirav,

I wanted to show you what I meant with my statement that you ae doing a "connectivity" test

With test I can check if a file exists. You have to test the result by verifying the variable $?

zrh1lw01[teuniss] test -f caramba ; echo $?
1
zrh1lw01[teuniss] test -f superkaramba-0.39.tar.gz | echo $?
0

In the first case, the file caramba does not exist, the test failed
The file superkaramba-0.39.tar.gz does exist hence my variable $? is 0


However, doing this with ssh (or rsh, does not matter) on a remote server you will get

zrh1lw01[teuniss] ssh zrh1l013 test -f superkaramba-0.39.tar.gz | echo $?
0
zrh1lw01[teuniss] ssh zrh1l013 test -f caramba | echo $?
0

Why? It does not matter what the contents of $? on the remote machine is. The value of $? is on the local machine and reflects the status of ssh. The ssh command ran. Hence the value of "0" ...
 
Old 01-18-2011, 10:27 AM   #10
cin_
Member
 
Registered: Dec 2010
Posts: 264

Rep: Reputation: 23
Special Characters

Bash Scripting Tutorial, Special Characters... http://tldp.org/LDP/abs/html/special-chars.html

Single and Double bracket explanation... http://tldp.org/LDP/abs/html/testcon....html#TTESTREF

Last edited by cin_; 01-18-2011 at 10:31 AM. Reason: gramm'err
 
Old 01-18-2011, 05:39 PM   #11
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Don't test. List and count:
Code:
for host in $list ; do
    count=`rsh $host "find /tmp /scratch -maxdepth 1 -type d -name '.X*' | wc -l"`
    if [ $count -gt 0 ]; then
        echo "$host: $count directories"
    else
        echo "$host: No directories"
    fi
done
Note that the counting is done on the remote machine, so only the command and the result is transferred.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 06:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to check whether mysql server is running or not vinaytp Linux - Newbie 3 01-18-2011 08:39 AM
ftp - check if file on remote exists (skip overriding) spiriad Linux - Newbie 5 07-29-2010 05:42 AM
How to call Shell Script on a remote server with remote servers env variables need Linux - Server 1 10-14-2009 08:37 PM
check if directory exists using shell script v333k Programming 9 04-23-2009 09:29 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM


All times are GMT -5. The time now is 08:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration