You are not logged in.

#1 2013-08-11 17:50:03

grobar87
Member
From: Македонија
Registered: 2010-08-25
Posts: 164

[SOLVED]Help with simple script modification

Hi,
I use the script below to notified me when battery is low and works great for now,but i want to remove acpi.So can someone help me to modify the script to use info from /sys/class/power_supply/BAT0* rather then acpi?

#!/bin/bash

BATTINFO=`acpi -b`
if [[ `echo $BATTINFO | grep Discharging` && `echo $BATTINFO | cut -f 5 -d " "` < 00:15:00 ]] ; then
    DISPLAY=:0.0 /usr/bin/notify-send "Baterijata e prazna!"
fi

Thanks.

Last edited by grobar87 (2013-08-17 15:26:30)


And... here... we... go!

Offline

#2 2013-08-11 20:17:30

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,024

Re: [SOLVED]Help with simple script modification

If you have a uevent file in that directory then all the relevant info should be in there.
What have you tried so far?


You're just jealous because the voices only talk to me.

Offline

#3 2013-08-13 05:39:15

at0m5k
Member
Registered: 2013-07-22
Posts: 40
Website

Re: [SOLVED]Help with simple script modification

I just threw this small script together for you. It loops until the battery is low (based on minutes remaining) and then sends a notification and exits. You might have to change the files that it reads from though because the files in /sys/class/power_supply/BAT0/* can vary. Hope it helps smile

#!/bin/bash
TIME=15 # Below TIME minutes, send notification.

while true; do
STATUS=$(cat /sys/class/power_supply/BAT0/status)
if [[ $STATUS == "Discharging" ]]; then
	NOW=$(cat /sys/class/power_supply/BAT0/energy_now)
	RATE=$(cat /sys/class/power_supply/BAT0/power_now)
	
	# Calculate minutes remaining using awk (expr can't handle floats!).
	MIN=$(awk -v now=$NOW -v rate=$RATE 'BEGIN { print 60 * now / rate }')
	# Use modulo as pseudo cast to integer.
	MIN=$(awk -v min=$MIN 'BEGIN { print min - min % 1 }');
	if [[ $MIN -lt $TIME ]]; then
		DISPLAY=:0.0 notify-send "Battery is low!"
		exit 0
	fi
fi
sleep 1;
done

Last edited by at0m5k (2013-08-13 16:03:03)

Offline

#4 2013-08-13 06:52:17

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 20,110
Website

Re: [SOLVED]Help with simple script modification


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#5 2013-08-13 08:13:57

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,024

Re: [SOLVED]Help with simple script modification


You're just jealous because the voices only talk to me.

Offline

#6 2013-08-13 16:17:15

at0m5k
Member
Registered: 2013-07-22
Posts: 40
Website

Re: [SOLVED]Help with simple script modification

Thanks Jason. I'm not very experienced with shell scripting (I code mostly in C) so that was some really good information to know smile I edited my earlier post so the code should be better now.

Nice script moetunes. Looks like he already has libnotify though, so if he wants something in C, it's an even easier implementation than drawing a window with Xlib tongue

Offline

#7 2013-08-17 15:26:04

grobar87
Member
From: Македонија
Registered: 2010-08-25
Posts: 164

Re: [SOLVED]Help with simple script modification

@at0m5k Your script works great! I just tried.
@moetunes Great program.I'll try this later definitely!
Thanks to all for help. smile


And... here... we... go!

Offline

Board footer

Powered by FluxBB