我可以使用bash命令替换技术在一个班轮中创建一个awk变量吗?这是我正在尝试的,但有些不对。awk -v是否接受命令替换?
awk -v AVG=$(uptime|awk '{print $(NF-2)}') '{ if (AVG >= 1) print "there is a load" }'
也许是因为命令替换使用了Awk(虽然我怀疑它)?也许这也是“先发制人”? GNU Awk 3.1.7
我可以使用bash命令替换技术在一个班轮中创建一个awk变量吗?这是我正在尝试的,但有些不对。awk -v是否接受命令替换?
awk -v AVG=$(uptime|awk '{print $(NF-2)}') '{ if (AVG >= 1) print "there is a load" }'
也许是因为命令替换使用了Awk(虽然我怀疑它)?也许这也是“先发制人”? GNU Awk 3.1.7
没有什么不对您的命令。你的命令正在等待输入,这就是它没有被执行的唯一原因!
例如:
$ awk -v AVG=$(uptime|awk '{print $(NF-2)}') '{ if (AVG >= 0) print "there is a load" }'
abc ## I typed.
there is a load ## Output.
只需在您的命令首先专家们的建议!
$ awk -v AVG=$(uptime|awk '{print $(NF-2)}') 'BEGIN{ if (AVG >= 0) print "there is a load" }'
there is a load
由于上一个awk命令没有输入文件,因此只能对该脚本使用BEGIN
子句。因此,您可以尝试以下操作:
awk -v AVG=$(uptime|awk '{print $(NF-2)}') 'BEGIN{ if (AVG >= 1) print "there is a load" }'
为什么在这里使用变量?至于AWK读取stdin
除非你明确指定相反,这应该是一个可取的方法:
$ uptime | awk '$(NF-2) >= 1 { print "there is a load" }'
there is a load
此:
awk -v AVG=$(uptime|awk '{print $(NF-2)}') '{ if (AVG >= 1) print "there is a load" }'
需要BEGIN正如其他人说:
awk -v AVG=$(uptime|awk '{print $(NF-2)}') 'BEGIN{ if (AVG >= 1) print "there is a load" }'
而且,你不需要调用两次AWK为可写为:
awk -v uptime=$(uptime) 'BEGIN{ n=split(uptime,u); AVG=u[n-2]; if (AVG >= 1) print "there is a load" }'
或更可能是你想要的:
uptime | awk '{ AVG=$(NF-2); if (AVG >= 1) print "there is a load" }'
可以简化为:
uptime | awk '$(NF-2) >= 1 { print "there is a load" }'
你能解决您的错字错误? (NF-2)缺少$ -sign。应该是$(NF-2)。 – alvits
@ user3088572,谢谢。固定。抱歉。 –
+1欢迎您。现在我可以投票了。 – alvits