Linux在回显语句中删除空间

时间:2015-06-16 22:24:25

标签: linux awk echo space

我正在尝试为类创建脚本,我的用户计数行出现问题。我无法弄清楚如何将2移近字符串。下面是它的样子,我的代码低于它!

[root@testit /root]# ./syswatch
Free/Total Memory : 261/320  MB
Free/Total Swap : 259/259 MB
User count is at       2 


#!/bin/sh
#
# Syswatch       Shows a variety of different task based on my Linux System
#
# description:   This script will first check the percentage of the filesystem
#                being used. If the percentage is above ___, the root user will
#                be emailed. There will be 4 things echoed in this script. The
#                first thing being the amount of free/total memory being used,
#                second the amount of free/total swap space being used, the
#                third is the user count, and the last thing is the amount
#                of time that the system has been up for and the system load.

#Prints amount of Free/Total Memory and Swap

free -t -m | grep "Total" | awk '{ print "Free/Total Memory : "$4"/"$2"  MB";}'
free -t -m | grep "Swap" | awk '{ print "Free/Total Swap : "$4"/"$2" MB";}'

#Displays the user count for the system

echo "User count is at `who | wc -l`"


exit 0

2 个答案:

答案 0 :(得分:0)

您可以使用printf

很好地控制格式
printf "User count is at %d\n" $(who | wc -l)

请注意,我用$()替换了反引号。这主要是一种风格决定,但很多人(包括我自己)都认为反击是最糟糕,可怕的事情,最好是降级到过去。

答案 1 :(得分:0)

我没有free因此我无法对其进行测试,但请尝试以下操作:

free -t -m |
awk -v who="$(who)" '
/Total/ { printf "Free/Total Memory : %s/%s MB\n", $4, $2 }
/Swap/  { printf "Free/Total Swap : %s/%s MB\n", $4, $2 }
END { print "User count is at", gsub(/\n/,"",who) }
'