Adding to torsmo display in dsl-n


Forum: DSL-N
Topic: Adding to torsmo display in dsl-n
started by: Juanito

Posted by Juanito on Sep. 26 2007,13:32
The recent worldtime thread motivated me to add some details to torsmo in dsl-n - I got most of the way there, but a little help from the scripting brethren would go a long way...

I modified .torsmorc to read:
Code Sample
${color red}$hr
${color grey}Uptime: � $color $uptime
${color #ddd}Battery: �$color ${battery}
${color #ddd}CPU Temp: $color ${acpitemp}degC
${color grey}CPU Freq: $color ${execi 180 ~/.cpufreq.sh}
${color grey}CPU Govn: $color ${execi 180 ~/.cpugov.sh}
${color green}$hr$color
[.cpufreq.sh]
cpufreq-info -mf
[.cpugov.sh]
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

The first problem I have is that cpufreq.sh/cpugov.sh return a value and a <cr> or <lf> or something which gives an empty line in the torsmo display. How do I prevent this?

The second problem I have is that I would like to add more data from a command with a couple of possible outcomes:
Code Sample
$ sudo /etc/init.d/laptop-mode status | grep drive
� �drive state is: �standby
(NOTE: drive settings affected by Laptop Mode cannot be retrieved.)

$ sudo /etc/init.d/laptop-mode status | grep drive
� �drive state is: �active/idle
(NOTE: drive settings affected by Laptop Mode cannot be retrieved.)

How do I retrieve "standby" or "active/idle" from this?

I realise the answer is out there in google but so far I just got confused...

Posted by ^thehatsrule^ on Sep. 26 2007,14:59
1
Can you edit those *.sh files? �It would be the ideal solution (look for echo or printf lines). �Otherwise if the extra newline is after the text you want, you can append ` | head -1 ` to get the first line of any stdout. �(actually I think the standard now is to use ` | head -n1 ` now, but both should work)

2
You probably want to use something more specific on your grep, such as ` | grep "drive state is:" `
then pipe that to some parsing utility... although you could probably implement everything in 1 command if you use a more powerful tool. �So in the end you could add something simple like ` | grep "drive state is:" | cut -21- ` or something more fancy.

An alternative to 2 is to look in those scripts in /etc/init.d and see where they get their info from... where it may be more efficient to parse input from (esp. if you are going to keep running it via torsmo).

Posted by Juanito on Sep. 27 2007,07:08
Quote
Can you edit those *.sh files?

- Sorry, I didn't explain myself well there. These are files I created to be able to pass the output of a command to torsmo (a la .torsmo_ip).

Thanks for the help, I'm moving nearer to a solution. If I use the commands directly from a terminal window, I get:
Code Sample
$ cpufreq-info -mf | head -n1
1.60 GHz
$ cpufreq-info -mf | head -c8
1.60 GHzdsl@dslbox:~$

So "head -n1" has no effect - "head -c8" works for 1.60 GHz but not for 800 MHz which has one character less.

Similiarly:
Code Sample
$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor | head -c11
performancedsl@dslbox:~$

...works with "performance" and "conservative" but not with "ondemand", "userspace" or "powersave"

Your suggestions for the grep statement worked well:
Code Sample
$ sudo /etc/init.d/laptop-mode status | grep "drive state is:" | cut -b22-
active/idle

but then I have the same problem of the output moving onto the next line.

I'll keep working on this - any additional suggestions would be welcome �:)

Posted by ^thehatsrule^ on Sep. 27 2007,13:33
Oh.. you don't want any newlines at all.. misunderstood you on that.  In your scripts, if you use echo, replace them with printf (or use the unused echo -n).

But in general you could remove newlines, or just the last character... such as with ` | head -c-1 `

Posted by Juanito on Sep. 27 2007,16:38
I can see that "echo -n" or printf would do the trick if I had the information I need in a variable (eg $PATH) - so I guess I need to figure out how to pass the output from a command into a variable. So far I can see how to pass a constant to a variable (eg VAR=hello, echo -n {$VAR})

| head -c-1 (and variations thereof) results in the error "head: -1: invalid number of bytes"

Edit: I'm guessing something like this will do it but I'm away from dsl-n at the moment:
Code Sample
$ sudo /etc/init.d/laptop-mode status | grep "drive state is:" | awk '{printf $1}'

Posted by ^thehatsrule^ on Sep. 27 2007,18:26
Afaik printf should be used over echo -n due to it being more standardized, (and therefore more portable).

Heh, maybe negatives aren't supported in the busybox version...

If you're using awk, you may as well do all of the parsing in it.

Posted by Juanito on Sep. 28 2007,07:19
The awk thing seems to have fixed the problem - the three mini-scripts now look like this:
Code Sample
$ cat .cpufreq.sh
#!/bin/sh
cpufreq-info -mf | awk '{printf $1 " " $2}'
#
$ cat .cpugov.sh
#!/bin/sh
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor | awk '{printf $1}'
#
$ cat .laptop_mode.sh
#!/bin/sh
sudo /etc/init.d/laptop-mode status | grep "drive state is:" | cut -b22- | awk '{printf $1}'
#

...and the excerpt from .torsmorc looks like this:
Code Sample
${color red}$hr
${color grey}Uptime:   $color $uptime
${color #ddd}Battery:  $color ${battery}
${color #ddd}CPU Temp: $color ${acpitemp} degC
${color grey}CPU Freq: $color ${execi 180 ~/.cpufreq.sh}
${color grey}CPU Govn: $color ${execi 180 ~/.cpugov.sh}
${color grey}/dev/hda: $color ${execi 180 ~/.laptop_mode.sh}
${color green}$hr$color


Quote
If you're using awk, you may as well do all of the parsing in it.

- after looking at the awk statement in .torsmo_ip I can believe you're right, but after staring at it for 30mins I wasn't feeling much enlightenment  :)

Posted by curaga on Sep. 28 2007,12:31
To pass the output to a var:
VAR=`echo Yeah`

Posted by WDef on Oct. 02 2007,13:31
In case it helps in future, to get rid of newlines in a pipe without invoking awk or sed or somesuch, do:

Code Sample
 echo 123 | tr -d '\n'


tr is probably more efficient (not that it probably matters much in this case).

Re:

Code Sample
sudo /etc/init.d/laptop-mode status | grep "drive state is:" | cut -b22- | awk '{printf $1}'


This can probably be contracted to:

Code Sample
sudo /etc/init.d/laptop-mode status | awk  '/drive state is/{printf $4}'

Posted by Juanito on Oct. 05 2007,07:45
Thanks - the 3 mini-scripts now look like this:
Code Sample
$ cat .cpufreq.sh
#!/bin/sh
cpufreq-info -mf | tr -d '\n'
#
$ cat .cpugov.sh
#!/bin/sh
cpufreq-info -p | awk '{printf $3}'
#
$ cat .laptop_mode.sh
#!/bin/sh
sudo /etc/init.d/laptop-mode status | awk '/drive state is/{printf $4}'
#

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.