bash中的脚本,用于在CPU超过时发送邮件

时间:2017-06-29 10:02:16

标签: linux bash

我有这个脚本:

Dim service As New CalendarService("cal1")
Dim entry As EventEntry = New EventEntry()

entry.Title.Text = Title

entry.Content.Content = Desc

Dim eventLocation As Where = New Where()


eventLocation.ValueString = Location
entry.Locations.Add(eventLocation)

'Dim dtstartdatetime As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 3, 30, 0)
Dim dtstartdatetime As DateTime = StartTime
'Dim dtenddatetime As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 4, 30, 0)
Dim dtenddatetime As DateTime = EndTime
Dim eventTime As [When] = New [When](dtstartdatetime, dtenddatetime)
entry.Times.Add(eventTime)

If (UserName <> "" And UserName.Length > 0) Then
    service.setUserCredentials(UserName, Password )
End If

Dim uripath As String = "http://www.google.com/calendar/feeds/" + UserName + "/private/full"

Dim postUri = New Uri("http://www.google.com/calendar/feeds/default/private/full")

postUri = New Uri(uripath)

Dim requestFactory As GDataGAuthRequestFactory = service.RequestFactory
requestFactory.CreateRequest(GDataRequestType.Insert, postUri)

Dim insertedEntry As AtomEntry = service.Insert(postUri, entry)

当我执行它时,我收到以下错误消息:

#!/bin/bash
# rsync using variables

CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //')

if [ $CPU -lt 100 ]
then
   cat mail_content.html | /usr/lib/sendmail -t
else
   echo "Normal"
fi

我希望它能够识别出现在没有发现的数字x.x(99.25)。

2 个答案:

答案 0 :(得分:1)

或者,如果CPU不是整数,您可以使用bc进行比较。

echo "$CPU < 100" | bc

如果结果为1,则CPU的值将小于100.所以你可以这样做:

[ $(echo "$CPU < 100" | bc) -eq 1 ] && echo yes

或者,您可以使用printf将其转换为整数:

printf '%.0f' "$CPU"

答案 1 :(得分:0)

问题是-eq不会比较浮点数,正如here所解释的那样。因此,您需要使用bc

#!/bin/bash
# rsync using variables

CPU=$(LANG=C sar 1 5 | grep "Average" | sed 's/^.* //')

if (( $(echo "$CPU  < 100" |bc -l) ))
then
   cat mail_content.html | /usr/lib/sendmail -t
else
   echo "Normal"
fi