LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2010, 12:28 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 608

Rep: Reputation: 34
Post remort loging, check for a file existince and display a message (if condition)


Hi I wan to ssh and check for a file, I hve came up with following, and its not working,

Is this possible or do I have to do this in a another way.

if [ [ `ssh root@myhost -C [ -a /vol1/backup/file1 ]` ]
then

echo "File exist"
else
echo "File not found"

fi


Thanks and bet regards

Last edited by procfs; 05-31-2010 at 12:29 AM.
 
Old 05-31-2010, 01:13 AM   #2
sohail0399
Member
 
Registered: Oct 2008
Location: Pakistan, Islamabad
Distribution: CentOS, Fedora, Solaris
Posts: 154

Rep: Reputation: 23
there is ] missing but there is still syntax error
 
Old 05-31-2010, 01:35 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Well... first to test a command you don't need the square brackets nor the back ticks, e.g.
Code:
if command
then
  do something
fi
here the exit status of the command is used as true (0 = success) or false (1 = failure). Second, I have some doubt about the ssh command you used. In openssh the -C option has a totally different meaning from "execute a command to the remote machine". What version of ssh are you using? Is this a linux box?

Here is what you can do if using openssh, to test the existence of a regular file on the remote machine:
Code:
if ssh user@host "test -f /path/to/remote/file"
then
  echo file exists
else
  echo file not found
fi
Hope this helps.
 
Old 05-31-2010, 01:58 AM   #4
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 608

Original Poster
Rep: Reputation: 34
Hi Guys, thanks for the reply, I've changed and removed the extra ] from the code, but even though the file exist it goes to echo "File does not exist" part

My OS is Redhat 5.4 and open ssh OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 (Sorry about that)

And my changed code is

if [ `ssh root@myhost -C "test -f /vol1/backup/file1"` ]
then

echo "File exist"
else
echo "File not found"

fi
 
Old 05-31-2010, 02:22 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Maybe my post was not clear enough, but you should end up with this:
Code:
if ssh root@myhost "test -f /vol1/backup/file1"
then
  echo "File exist"
else
  echo "File not found"
fi
The -C option of ssh means "Requests compression of all data (including stdin, stdout, stderr, and data for forwarded X11 and TCP connections)" and most likely it is not necessary. See man ssh for details.

Regarding the syntax of if/then to test commands (not expressions) see here, where you read
Quote:
An if can test any command, not just conditions enclosed within brackets.
 
Old 05-31-2010, 02:32 AM   #6
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 608

Original Poster
Rep: Reputation: 34
Hi colucix, you are right, I understood your answer wrong, thanks for the help its working

Bast Regards
 
  


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
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
[SOLVED] Root file sys check on JFS during boot fails with e2fsck message catkin Slackware 2 02-21-2010 05:29 AM
SCSI Error: (0:4:0) Status=02h (CHECK CONDITION) tss23 Linux - Newbie 1 04-10-2008 08:07 AM
abnormal behavious in comparison check in 'if' condition. indiancosmonaut Programming 10 04-03-2008 12:10 AM
Loging on using nfs and file permission question. synapse Slackware 1 12-20-2007 07:50 AM


All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration