简体   繁体   中英

running ssh command from a shell script

I am new to shell scripting , I am trying to run the following command from shell script .

 diff <(ssh user@remote_host 'cat remote_file.txt') <(ssh user2@remote_host2 'cat remote_file2.txt')

but getting an error :

./a.sh: syntax error at line 1: `(' unexpected

I tried some example trough googleing which says to use $() around the code , but it did not work can any one please help me with this .

I understand you want to use the output from the two remote files in a 'diff'. There are many things wrong in your solution:

  • You cannot redirect two output streams into one input stream
  • diff only works on files, not on stdin
  • using the output from one command as the input for another command is done using a pipe. Redirects are either from file to command or from command to file.
  • parenthesis are used to bundle commands, for instance "( echo a ; echo b ) | cat" will cause the output of 'echo a' and 'echo 'b' to be passed in one stream to the input of 'cat', which will simply print it.

What I would do (to accomplish what I think you want to do) is turn it into three seperate commands:

ssh user@remote_host 'cat remote_file.txt' > file1
ssh user2@remote_host2 'cat remote_file.txt' > file2
diff file1 file2

I just make this bash script without problems

#!/bin/bash
diff <(ssh user@remote_host cat remote_file.txt) <(ssh user2@remote_host2 cat remote_file2.txt)
exit 0

this work with the next conditions:
-remote_host and remote_host2 stay in the list of ~/.ssh/known_hosts
-user and user2 exists and have permissions
-remote_host and remote_host2 are operative and have ssh server on
-user@remote_host and user2@remote_host2 have configured ssh for work without passwords
if you not know how do this see http://www.linuxproblem.org/art_9.html

maybe your error stay in ' but it must work well if you don't use 'remote_file.txt' and only use remote_file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM