You are not logged in.
Pages: 1
Hi everyone.
when i do
cat /sys/devices/platform/smapi/BAT0/remaining_running_time
i get a numeric value with 1 to 3 digits.
How can i divide the output of that file by 60 using a pipe?
Greets,
demian
no place like /home
github
Offline
bc?
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
There is probably a cleaner way to do this, but:
$ echo "$(cat /sys/devices/platform/smapi/BAT0/remaining_running_time)/60" | bc -l
cut, sed, or awk might be useful if you want to remove trailing zeroes.
(Credit goes to this page for reminding me that the -l flag enables standard division.)
#!/vim/rocks
Offline
Thanks.
I probably should have mentioned that I want to use this within a conky configuration file. So, while your code works fine with interpreter i have trouble implementing it in conky.
no place like /home
github
Offline
Did you try exec?
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
Geez. Of course. In my defense, it's 6.30 am here.
The full command:
Remaining: ${color1}${exec echo "$(cat /sys/devices/platform/smapi/BAT0/remaining_running_time)/60" | bc -l | cut -c1-3}h${color}
Last edited by demian (2010-05-08 04:28:12)
no place like /home
github
Offline
a version without bc
cat /sys/devices/platform/smapi/BAT0/remaining_running_time | awk '{print $1/60}'
Offline
Thanks.
Remaining: ${color1}${exec cat /sys/devices/platform/smapi/BAT0/remaining_running_time | awk '{print $1/60}'}h${color}
Works just as well.
no place like /home
github
Offline
But not that awk use more resource than bc. Timing then (this is only a lazy comparison)
$ time (for i in {1..1000}; cat remaining_running_time|awk '{print $1/60}' >/dev/null)
(; for i in {1..1000}; do; cat remaining_running_time | awk '{print $1/60}' >) 2.49s user 1.32s system 80% cpu 4.717 total
$ time (for i in {1..1000}; echo "$(cat remaining_running_time)/60"|bc -l >/dev/null)
(; for i in {1..1000}; do; echo "$(cat remaining_running_time)/60" | bc -l >) 0.54s user 0.62s system 32% cpu 3.560 total
And for comparison a small "made in 10 seconds" c program:
$ time (for i in {1..1000}; ./read remaining_running_time 60 >|/dev/null)
(; for i in {1..1000}; do; ./read remaining_running_time 60 >| /dev/null; done 0.04s user 0.20s system 19% cpu 1.210 total
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp = fopen(argv[1],"r");
float time;
fscanf(fp, "%f", &time);
printf("%f\n", time/atof(argv[2]));
fclose(fp);
return 0;
}
Offline
$ echo $(( 60 / 3 ))
20
Offline
$ echo $(( 60 / 3 ))
20
The problem is conky call sh and sh is bash and bash dont like floating points (try $((60.0/3))), zsh run floating point ok. But with integer arithmetic is ok this is a solution
Offline
Thanks for th hint, kazuo. The difference in speed and resource consumption is impressive.
no place like /home
github
Offline
Pages: 1