[SOLVED] remort loging, check for a file existince and display a message (if condition)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.