LinuxQuestions.org
Register a domain and help support LQ
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 08-01-2011, 02:11 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 103

Rep: Reputation: 15
SSH commands and if statements


So I want to run command through ssh but also run a if check in bash to see if a file exist. I know that to run ssh commands you do ssh user@server YOURCOMMAND

but if i need to run an if statements, how would this work??
 
Old 08-01-2011, 02:31 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
It should work exactly as you mentioned, example:
Code:
ssh user@host "if [[ -f /path/to/some/file ]]; then echo file exists; else echo file not found; fi"
 
Old 08-01-2011, 02:35 PM   #3
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Hello,

I used to use a small quick and dirty script in the past for something like this..... I just ran the ssh command along with 1>temp1.txt, and I tested the output against regular expressions. There are many ways to do this, it's all about how you want to tackle this. You could also just ssh into your remote server, and use a expect script of some sort to do your dirty work.

Cheers,

Josh
 
Old 08-01-2011, 02:37 PM   #4
kailash19
LQ Newbie
 
Registered: Apr 2010
Location: Pune
Posts: 13

Rep: Reputation: 0
You can add she bang line in front for bash shell to sure.
ssh user@host "#!/bin/sh if [[ -f /path/to/some/file ]]; then echo file exists; else echo fi
 
Old 08-01-2011, 02:49 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by kailash19 View Post
You can add she bang line in front for bash shell to sure.
ssh user@host "#!/bin/sh if [[ -f /path/to/some/file ]]; then echo file exists; else echo fi
This cannot work, first because the ! is interpreted as a shell's history call, second - even if you protect the ! using single quotes to embed the command - the sha-bang in interactive mode will be interpreted as a comment and the whole line will be ignored. The sha-bang is correctly interpreted only inside scripts.
 
  


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
SSH with commands fusion1275 Programming 2 03-04-2009 05:14 AM
Log all SSH commands at ssh-client. mohammednv Linux - Networking 6 01-26-2008 05:02 AM
ssh remote commands dcfnef Linux - Security 1 11-15-2007 11:39 AM
Many commands using ssh Corrado Linux - General 4 05-10-2007 01:05 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM


All times are GMT -5. The time now is 10:41 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