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 01-08-2013, 12:38 PM   #1
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Rep: Reputation: Disabled
if statement on remote machine


Hi,

I am just checking the directory if it exists . Can anybody help me to resolve the below syntax ?

[root@localhost ~]# for i in `cat /tmp/ip `; do echo $i ; ssh $i " if [ -d /tmp/test ]; then echo 'hi it is done ' else echo ' Not done ' fi "; done
localhost
root@localhost's password:
bash: -c: line 1: syntax error: unexpected end of file


Thanks,

Last edited by project.linux.proj; 01-08-2013 at 12:39 PM.
 
Old 01-08-2013, 12:43 PM   #2
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
Can you repost the entire statement between code tags?
I think you need a semicolon just before the "fi".

Last edited by lykwydchykyn; 01-08-2013 at 12:47 PM.
 
Old 01-08-2013, 12:52 PM   #3
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Original Poster
Rep: Reputation: Disabled
[root@localhost ~]# for i in localhost ; do echo $i ; ssh $i " if [ -d /tmp/test ]; then echo 'hi it is done ' else echo ' Not done ' fi "; done
localhost
root@localhost's password:
bash: -c: line 1: syntax error: unexpected end of file


I tried semicolon but didn't work


Thanks,
Mohit
 
Old 01-08-2013, 12:55 PM   #4
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
You tried this?
Code:
for i in localhost ; do echo $i ; ssh $i " if [ -d /tmp/test ]; then echo 'hi it is done '; else echo ' Not done '; fi "; done
Every complete statement either needs a newline or a semicolon.
 
1 members found this post helpful.
Old 01-08-2013, 01:01 PM   #5
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Original Poster
Rep: Reputation: Disabled
Thanks buddy it really worked. I will check now more complex syntaxes.


regards,
 
Old 01-09-2013, 01:48 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
A few other points for you, concerning the command posted in the OP:

Code:
for i in `cat /tmp/ip `; do echo $i ; ssh $i " if [ -d /tmp/test ]; then echo 'hi it is done ' else echo ' Not done ' fi "; done
0) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


1) Don't Read Lines With For!!


2) Useless Use Of Cat Backticks (really the same as DRLWF).


3) $(..) is highly recommended over `..`, anyway.


4) QUOTE ALL OF YOUR VARIABLE EXPANSIONS!

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


5) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://mywiki.wooledge.org/ArithmeticExpression


6) Store the commands you want to send in a variable first*. That will improve readability and make protecting it from the current shell much easier (assuming you quote it properly).

(caveat: I've never used ssh personally, so I have no idea if the given syntax is correct)

Code:
commands='if [[ -d /tmp/test ]]; then echo "hi it is done" ; else echo "Not done" ; fi'

while read -r server; do echo "$server" ; ssh "$server" "$commands" ; done  </tmp/ip

*Note that usually this is a bad idea. But in this case the "commands" are actually data to be manipulated and sent somewhere else, rather than code for the shell to run.
 
  


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
how to know the remote machine is Virtual machine or physical machine pantdk Linux - Server 19 10-16-2014 02:48 PM
[SOLVED] SSH remote command: Pipe remote output to local machine? kenneho Linux - Server 6 12-06-2012 02:37 AM
FTP: Get count of files on a remote machine directory to the source Linux machine mamu Linux - Newbie 3 10-28-2011 05:10 PM
data shift from 1 remote machine to another remote machine raheelch General 7 03-09-2010 09:44 AM
mounting samba filesystem on remote machine as /home on local machine shishirkotkar Linux - Software 1 04-28-2008 06:05 AM


All times are GMT -5. The time now is 12:48 AM.

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