我需要确定系统上的用户数,如果该值大于或等于脚本中设置的用户计数变量,则打印出系统已运行的时间以及系统负载是多少。我如何将其添加到我的 echo 中?在我的代码中
#!/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
printf "User count is at %d\n" $(who | wc -l)
count=$(who | wc -l)
if [ $count -eq 2 ]
then
echo "The system has been up for _______ with a system load of average: __"
fi
exit 0
答案1
uptime
提供您要查找的信息,因此您可以直接调用它而不是echo
:
> uptime
23:40pm up 13 days 8:09, 6 users, load average: 1.28, 1.25, 1.23
如果格式不令人满意,你可以echo
用类似以下内容替换该语句:
uptime | sed 's/.*up/The system has been up for/' | sed 's/,.*load/ with a system load/'
或者,如果您真的想使用,echo
您可以解析uptime
输出以获取您想要的值(就像您对 所做的那样$count
)并在 echo 语句中使用它们。
附注:
- 一旦您可以重新排列代码以便不再调用它,您就已经获得了用户数:
count=$(who | wc -l) printf "User count is at %d\n" $count
- “大于或等于”运算符
-ge
不是-eq
:
如果[$count-ge 2]