fixed parsing for uptime

This commit is contained in:
Russel Yasol
2024-05-13 21:22:25 +08:00
parent 06b84b48a4
commit 6f96491273
2 changed files with 12 additions and 43 deletions

View File

@@ -1,50 +1,11 @@
#!/bin/bash
# Execute the uptime command and extract the uptime duration
# Execute the uptime command and store the result
uptime_output=$(uptime)
# Example output of the uptime command
# 2 days, 14:08,
# Extract days, hours and minutes from the uptime string
# For days, match if day or days is present in the string
if [[ "$uptime_output" =~ day ]]; then
days=$(echo "$uptime_output" | awk -F '[ ,]+' '{print $2}')
else
days=0
fi
hours=$(echo "$uptime_output" | awk -F '[ :]+' '{print $6}')
minutes=$(echo "$uptime_output" | awk -F '[ :]+' '{print $7}')
# Check if days is empty, then set it to 0
if [ -z "$days" ]; then
days=0
fi
# Remove comma to days
days=$(echo $days | tr -d ,)
# Remove comma to minutes
minutes=$(echo $minutes | tr -d ,)
# Create a text response with the uptime duration. If days is empty, then set it to 0
uptime_text="$days days, $hours hours, $minutes minutes"
# If days is 0, then remove it from the text response
if [ "$days" -eq 0 ]; then
uptime_text="$hours hours, $minutes minutes"
fi
# if days and hours are 0 or 1, remove s from the end of the string
if [ "$days" -eq 0 ] && [ "$hours" -eq 0 ] || [ "$days" -eq 1 ] && [ "$hours" -eq 1 ]; then
uptime_text=$(echo $uptime_text | sed 's/s//g')
fi
# Set header for plain text content
echo "Content-Type: text/plain"
echo ""
# Output the text response
echo "$uptime_text"
# Output the uptime result
echo "$uptime_output"

View File

@@ -1212,7 +1212,15 @@
fetch("/cgi-bin/get_uptime")
.then((response) => response.text())
.then((data) => {
this.uptime = data;
// Split the data by space
const uptimeArray = data.split(" ");
console.log(uptimeArray);
// If uptimeArray[3] is not empty, then set the uptime for days
if (uptimeArray[3] !== "") {
this.uptime = uptimeArray[3] + " days, " + uptimeArray[5].split(":")[0] + " hours, and " + uptimeArray[5].split(":")[1].replace(",", "") + " minutes";
} else {
this.uptime = uptimeArray[4].split(":")[0] + " hours and " + uptimeArray[4].split(":")[1].replace(",", "") + " minutes";
}
});
},