JosephKisanue
JosephKisanue

Reputation: 13

Passing a bash command to a remote host using ssh

Doing a migration my bit of code is

cat /etc/fstab |grep nfs >/root/mounts.txt
cat /etc/fstab |grep cifs >> /root/mounts.txt
rsync -av /root/mounts.txt                              ${REMOTEHOST}:/root/
ssh root@${REMOTEHOST} 'cat /root/mounts.txt >> /etc/fstab'
ssh root@${REMOTEHOST} 'for i in $(cat /root/mounts.txt |awk '{print $2}');do mkdir -p $i; done'

Problem is that the last line works locally:

 for i in $(cat /root/mounts.txt |awk '{print $2}');do mkdir -p $i; done 

However when I am passing it to the remote host I am getting:

" {print

awk: cmd. line:1: ^ unexpected newline or end of string"

Any suggestions to fix it?

Upvotes: 1

Views: 129

Answers (1)

fedorqui
fedorqui

Reputation: 289915

This is because the command provided in the ssh gets stopped when writing the first quote of the awk expression.

So you may want to use another approach:

ssh root@${REMOTEHOST} 'while read -r _ host _; do mkdir -p $host; done < /root/mounts.txt'

This uses a while read variable1 variable2 variable3 so that you don't need to use awk to get the second value.

Upvotes: 2

Related Questions