익명 08:20

Why is my command to get the host value with awk not working as expected?

Why is my command to get the host value with awk not working as expected?

Why does this Awk command:

iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")"
echo $iphost

Display:

g-3.xx.yyy.zz has address 172.16.65.35

Instead of:

172.16.65.35


Top Answer/Comment:

You should use escape sequence backslash(\) inside awk command as below. Since you use the command inside double quotes the variables will be resolved before executing. So it will try to understand $4 as a system variable and try to resolve it. If you use escape sequence backslash it will retain the $4 to the awk command.

iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print \$4 }'")"
echo $iphost
상단 광고의 [X] 버튼을 누르면 내용이 보입니다