#!/bin/sh
read IP (Example: 192.168.2.46)
var1= $IP
awk -v var="$var1" 'NR==3{print " \"server\": [ \"var:666\" ]"}7' /home/mark/server.conf > /home/mark/tmp && mv -f /home/mark/tmp /home/mark/server.conf
所需的输出是:
"server": [ "192.168.2.46:666" ]
但是,我得到了:
"server": [ "var1:666" ]
为什么以及如何解决?
答案 0 :(得分:0)
只需将awk变量var
放在双引号之外。
awk -v var="$var1" 'NR==3{print " \"server\": [ \""var":666\" ]"}7'
示例:
$ var1="123"
$ echo '1' | awk -v var="$var1" '/1/{print "foo\""var"\"bar"}'
foo"123"bar