我正在尝试教自己一些基本的脚本,并且无法正确使用awk命令。我想通过free -m命令显示当前可用的可用内存和总内存,以MB为单位。但是,我一直收到将在下面发布的错误。为什么是这样?
syswatch:
#!/bin/bash
# Use free command to show current free and total memory available in MB
free -m | awk `{print "Free/Total Memory : $3 / $1 MB"}`
输出/错误:
[root@testit /root]# ./syswatch
./syswatch: {print: command not found
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W help --help
-W lint --lint
-W lint-old --lint-old
-W posix --posix
-W re-interval --re-interval
-W source=program-text --source=program-text
-W traditional --traditional
-W usage --usage
-W version --version
To report bugs, see node `Bugs' in `gawk.info', which
is section `Reporting Problems and Bugs' in the
printed version.
预期输出:
Free/Total Memory : 133/190 MB
答案 0 :(得分:1)
似乎您想要百分比
free -m | sed 1d | awk '{print "Free/Total Memory ", $1, " ", $3 / $2}'
您的问题
free
输出的标题:sed 1d
删除第一行$1
是内存类型,$2
是总数,$3
是已用内存如果/
符号不是除法符(请参阅您的评论),则
free -m | sed 1d | awk '{printf "Free/Total Memory %s %s/%sMB\n", $1, $3, $2}'