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, |
Can you repost the entire statement between code tags?
I think you need a semicolon just before the "fi". |
[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 |
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 |
Thanks buddy it really worked. I will check now more complex syntaxes.
regards, |
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 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' *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. |
All times are GMT -5. The time now is 02:58 PM. |