sshsh

Multi-line command execution over remote SSH


I want to execute multiple lines of shell commands over remote ssh.

According to https://unix.stackexchange.com/questions/1459/remote-for-loop-over-ssh, I just need to use single quotes to execute a multi-line for loop. Here is what I tried:

ssh user@server ‘cd ~/Data; cwd=pwd; for i in `find 201806 -name "day_*"`; do echo $i; cd $i; a.sh; cd $cwd; done’

Since nothing happens, I am speculating that there is a syntax error that I must not be understanding. 201806 is the name of a folder in the Data directory, and I have tested that the command works without the ssh user@server. Any suggestions?


Solution

  • Try this

    ssh -v user@server ‘cd ~/Data; cwd=`pwd`; for i in `find 201806 -name "day_*"`; do echo $i; cd $i; ./a.sh; cd $cwd; done’
    

    Also, make sure your a.sh file have execute permission. -v option will give debugging messages about its progress.