简体   繁体   English

awk +运行awk命令而不从管道输出

[英]awk + run awk command without output from pipe line

the target of the following code is to verify if the free memory is less then 20% of the total memory 以下代码的目标是验证可用内存是否少于总内存的 20%

in case fr ee memory less then awk should print FAIL otherwise awk will print ok 万一内存不足,则awk应该打印失败,否则awk将打印成功

TOTALM=120000K
FREEM=89111K

please advice what wrong in this syntax? 请告知此语法有什么问题? , why I cant run awk as displayed here? ,为什么我不能按此处显示的方式运行awk?

awk -v FREEM=$FREE_MEMORY -v TOTALM=$TOTAL_MEMORY '{per=int(FREEM)/int(TOTALM)*100; if(per<=20) print "FAIL" ; else print "OK"}'  

You have a couple of problems in your code: 您的代码中有几个问题:

Firstly, you have changed the names of your two variables when passing them into awk . 首先,将两个变量传递给awk时,已经更改了它们的名称。

Secondly, awk needs an input file after the script, but you can get around that requirement by putting your code inside a BEGIN block. 其次, awk在脚本之后需要一个输入文件,但是您可以通过将代码放入BEGIN块中来解决该要求。

#!/bin/bash
TOTALM=120000K
FREEM=89111K
awk -v FREEM=$FREEM -v TOTALM=$TOTALM '
           BEGIN {per=int(FREEM)/int(TOTALM)*100
                  if(per<20) print "FAIL"
                  else print "OK"}' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM