1

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?

1
  • 1
    Your "inner" single quotes are not "inner" at all. They are ending (and restarting) the outer single quoted string. You need to escape them. Replace them with '\'' (single-quote backslash single-quote single-quote). Commented Apr 26, 2015 at 17:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that's better approach +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.