Customizing Script's Print Output

Specs: Current Manjaro/xfce/One Netbook A1

The A1 has a battery bug (80% is full charge, but machine can work at 0% for another 20%). I’ve opened another thread to wonder if this output can be customized to use negative numbers for battery percentage.

In the meantime, I get 4 hours of battery usage regardless of what the percentage says, so that could be a workaround. To be accurate, I would need to subtract suspend time from uptime, and this script works with Manjaro ( https://askubuntu.com/questions/321855/how-to-get-real-uptime/1186845#1186845 ):

#!/bin/bash

# NAME: suspendtime
# PATH: $HOME/askubuntu/
# DESC: For: https://askubuntu.com/questions/321855/how-to-get-real-uptime
# DATE: November 6, 2019.

# NOTE: Calculate suspend time from systemd's journalctl

# UPDT: 2019-11-07 Fine-tune removing 0 Units in DaysMinutesStr
# UPDT: 2020-05-05 works with Ubuntu 20.04 but has problem with changing monthes

# Duplicate DaysMinutes from ~/.bashrc for Ask Ubuntu
DaysMinutes () {

    local d h m s
    (( d = ${1} / 86400 ))
    (( h = (${1}%86400) / 3600 ))
    (( m = (${1}%3600) / 60 ))
    (( s = ${1}%60 ))
    DaysMinutesStr="$d days, $h hours, $m minutes, $s seconds"

    # Convert 1's to singular
    [[ ${DaysMinutesStr:0:2} = "1 " ]] && \
        DaysMinutesStr="${DaysMinutesStr/days/day}"
    DaysMinutesStr="${DaysMinutesStr/ 1 hours/ 1 hour}"
    DaysMinutesStr="${DaysMinutesStr/ 1 minutes/ 1 minute}"
    DaysMinutesStr="${DaysMinutesStr/ 1 seconds/ 1 second}"

    # Suppress zero strings
    [[ ${DaysMinutesStr:0:1} = "0" ]] &&
        DaysMinutesStr="${DaysMinutesStr/0 days, / }"
    DaysMinutesStr="${DaysMinutesStr/ 0 hours, / }"
    DaysMinutesStr="${DaysMinutesStr/ 0 minutes, / }"
    DaysMinutesStr="${DaysMinutesStr/, 0 seconds/}"
} # DaysMinutes

# Build array of suspend cycles from Systemd
IFS=
```\n' Arr=( $(journalctl -b-0 | grep -E 'PM: suspend' | cut -c5-15) )

[[ ${#Arr[@]} -gt 0 ]] && upper=$(( ${#Arr[@]} - 1 ))
SuspentCount=0
[[ $upper -gt 0 ]] && for (( i=0; i<upper; i=i+2 )) ; do
    (( SuspendCount++ ))
    Time=$(( $(date +%s -d "2020-01-${Arr[i+1]}") - $(date +%s -d "2020-01-${Arr[i]}") ))
    SuspendTime=$(( SuspendTime + Time ))
    printf "%s to %s lasting %'d seconds\n" "${Arr[i]}" "${Arr[i+1]}" "$Time"
done

echo
LinuxTime=$(( $(date +%s -d "Now") - $(date +%s -d "$(uptime -s)") ))
DaysMinutes "$LinuxTime"
printf "Linux uptime \t%'d seconds (%s)\n" "$LinuxTime" "$DaysMinutesStr"
if [ $SuspendCount >  0 ]; then 
    DaysMinutes "$SuspendTime"
    printf "%sx Suspend \t%'d seconds (%s)\n" "$SuspendCount" "$SuspendTime" "$DaysMinutesStr"
fi
RealTime=$(( LinuxTime - SuspendTime ))
DaysMinutes "$RealTime"
printf "Real uptime \t%'d seconds (%s)\n" "$RealTime" "$DaysMinutesStr"

Producing:

19 15:02:44 to 19 15:09:29 lasting 405 seconds

Linux uptime 	2,762 seconds ( 46 minutes, 2 seconds)
1x Suspend 	405 seconds ( 6 minutes, 45 seconds)
Real uptime 	2,357 seconds ( 39 minutes, 17 seconds)

Using xfce’s genmon applet, I would like to cut the above output down to “Real uptime”'s minutes/hours, as in:

39m

I’ve experimented with different edits, but I keep borking the script. May I ask for the proper syntax?

Thank you.

So you want just the Real uptime in minutes?
Here we go:

#!/bin/bash

# NAME: suspendtime
# PATH: $HOME/askubuntu/
# DESC: For: https://askubuntu.com/questions/321855/how-to-get-real-uptime
# DATE: November 6, 2019.

# NOTE: Calculate suspend time from systemd's journalctl

# UPDT: 2019-11-07 Fine-tune removing 0 Units in DaysMinutesStr
# UPDT: 2020-05-05 works with Ubuntu 20.04 but has problem with changing monthes

# Build array of suspend cycles from Systemd
IFS=$'\n' Arr=( $(journalctl -b-0 | grep -E 'PM: suspend' | cut -c5-15) )

[[ ${#Arr[@]} -gt 0 ]] && upper=$(( ${#Arr[@]} - 1 ))
SuspentCount=0
[[ $upper -gt 0 ]] && for (( i=0; i<upper; i=i+2 )) ; do
    (( SuspendCount++ ))
    Time=$(( $(date +%s -d "2020-01-${Arr[i+1]}") - $(date +%s -d "2020-01-${Arr[i]}") ))
    SuspendTime=$(( SuspendTime + Time ))
done

echo
LinuxTime=$(( $(date +%s -d "Now") - $(date +%s -d "$(uptime -s)") ))
RealTimeMin=$((( LinuxTime - SuspendTime ) / 60 ))
printf "%'dm\n" "$RealTimeMin"
1 Like

Fantastic. I wish you had designed the actual battery; I would have many fewer problems :slight_smile:

Could I ask you to change the syntax output to something akin to:

2h 33m

Thank you for weighing in.

I dont know if that echo in the middle is there on purpose.
(it creates an extra line before the desired text output)

Anyhoo some quick extra arithmetic can do that…

#!/bin/bash

# NAME: suspendtime
# PATH: $HOME/askubuntu/
# DESC: For: https://askubuntu.com/questions/321855/how-to-get-real-uptime
# DATE: November 6, 2019.

# NOTE: Calculate suspend time from systemd's journalctl

# UPDT: 2019-11-07 Fine-tune removing 0 Units in DaysMinutesStr
# UPDT: 2020-05-05 works with Ubuntu 20.04 but has problem with changing monthes

# Build array of suspend cycles from Systemd
IFS=$'\n' Arr=( $(journalctl -b-0 | grep -E 'PM: suspend' | cut -c5-15) )

[[ ${#Arr[@]} -gt 0 ]] && upper=$(( ${#Arr[@]} - 1 ))
SuspentCount=0
[[ $upper -gt 0 ]] && for (( i=0; i<upper; i=i+2 )) ; do
    (( SuspendCount++ ))
    Time=$(( $(date +%s -d "2020-01-${Arr[i+1]}") - $(date +%s -d "2020-01-${Arr[i]}") ))
    SuspendTime=$(( SuspendTime + Time ))
done

echo
LinuxTime=$(( $(date +%s -d "Now") - $(date +%s -d "$(uptime -s)") ))
RealTimeMin=$((( LinuxTime - SuspendTime ) / 60 ))
((hour=$RealTimeMin/60))
((min=$RealTimeMin-$hour*60))
printf ""$hour"h "$min"m\n"
1 Like

The last script does it, exactly.

I don’t know how you folks pull these things off–I can follow instructions, be appreciative of said instructions, I can play with your instructions to try and generate the needed syntax, but I can’t do it until you folks weigh back in.

I’m so appreciative, and so appreciate the speed of help.

At any rate, I would like to mark this solved, but I would like to mark both posts as solutions. What should I do?

Thank you, thank you.

1 Like

mark cscs, 'cause he posted the final solution…i’m fine with it :wink:

2 Likes

I’ll do that, but it seems the credit should be shared.

Bounce
you posted the first solution to the title question, I only revised it according to a follow-up :wink:

1 Like

See? I knew you both were right :slight_smile:

1 Like

This topic was automatically closed 3 hours after the last reply. New replies are no longer allowed.