Эхо безотказной работы в Linux

834
Nicole Romain

Мне нужно определить количество пользователей в системе, и, если значение выше или равно переменной числа пользователей, установленной в скрипте, распечатать, как долго работала система и какова нагрузка на систему. Как мне добавить это в мое эхо? Здесь, в моем коде

#!/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

1 ответ на вопрос

1
Dan Cornilescu

uptime provides the info your looking for, so you could just call it instead of echo:

> uptime 23:40pm up 13 days 8:09, 6 users, load average: 1.28, 1.25, 1.23 

If the format is not satisfactory you could replace the echo statement with something like:

uptime | sed 's/.*up/The system has been up for/' | sed 's/,.*load/ with a system load/' 

Or if you really want to use echo you could parse the uptime output to get the values you want (like you do for $count) and use them in the echo statement.

Side notes:

  • you're already getting the user count once you could re-arrange the code to not call it again:
count=$(who | wc -l) printf "User count is at %d\n" $count 
  • the 'greater or equal' operator is -ge not -eq:

if [ $count -ge 2 ]

Похожие вопросы