PDA

View Full Version : [SOLVED] Conky script programming issues



Mahngiel
January 10th, 2010, 04:32 AM
I am trying to get going on some script writing following the precedent set by a script I picked up. It's best if you read the coding first:



#! /bin/bash
function net()
{
echo $connect '% Connection'
}
function mem()
{
echo $ram 'total RAM'
}

connect=$(iwconfig wlan0|grep 'Link Quality='|grep '='|grep --max-count=1 -o '\=\([0-9]\+\)'|grep --max-count=1 -o '\([0-9]\+\)')
ram=$(top|grep -m 1 'Mem:'|grep 'total'|grep --max-count=1 -o '[0-9]\{4,\}'|grep -m 1 '[0-9]\{4,\}')
pr
net && mem

exit 0
What this is supposed to do is output the percentage of network link quality, followed by my total ram memory. If you terminal this information, you can see why it should work:



king@mahngiel:~$ iwconfig wlan0
wlan0 IEEE 802.11bg ESSID:"goesh"
Mode:Managed Frequency:2.412 GHz Access Point: 00:0F:61:FE:B3:B0
Bit Rate=11 Mb/s Tx-Power=20 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Power Management:off
Link Quality=60/70 Signal level=-50 dBm Noise level=-66 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
king@mahngiel:~$ iwconfig wlan0|grep 'Link Quality='|grep '='|grep --max-count=1 -o '\=\([0-9]\+\)'|grep --max-count=1 -o '\([0-9]\+\)'
57

king@mahngiel:~$ top
top - 22:21:38 up 10:04, 3 users, load average: 0.17, 0.23, 0.17
Tasks: 193 total, 1 running, 177 sleeping, 15 stopped, 0 zombie
Cpu(s): 2.5%us, 2.2%sy, 0.0%ni, 95.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 505000k total, 469648k used, 35352k free, 38828k buffers
Swap: 1485972k total, 19180k used, 1466792k free, 194056k cached

king@mahngiel:~$ top|grep -m 1 'Mem:'|grep 'total'|grep --max-count=1 -o '[0-9]\{4,\}'|grep -m 1 '[0-9]\{4,\}'
505000
The problem is, net() will post display, but mem() has no output.

I don't know if it's due to how conky inherently defines its objects to call from, or what. The way I see it, the conky variable for your net is ${wireless_link_qual_perc}, so there shouldn't be a reason why conky could understand the first script reference to iwconfig wlan0.

Any input here would be greatly appreciated.

Brandon Williams
January 10th, 2010, 07:37 PM
Is the line that reads 'pr' really in your script? That's the only thing that doesn't work for me, and I doubt it's expected to be there.

Mahngiel
January 10th, 2010, 10:11 PM
Is the line that reads 'pr' really in your script? That's the only thing that doesn't work for me, and I doubt it's expected to be there.

Maybe not, but it does work. Like I said, i'm new to this scripting idea. Anyways, i plopped another net() in there so you can see the space is reserved...

kaivalagi
January 10th, 2010, 10:38 PM
Can you just try this in a script file (/tmp/mem.sh):


#!/bin/sh
mem=`top|grep -m 1 'Mem:'|grep 'total'|grep --max-count=1 -o '[0-9]\{4,\}'|grep -m 1 '[0-9]\{4,\}'`
echo $mem

I am assuming your problem is that something like the above isn't outputting in conky? i.e. the below in a conkyrc:

[${execi 5 sh /tmp/mem.sh}]
This just gives this:

[]

If run at the command line you get something sensible:

4054116

I have other things on but I think you need to look at stdout redirection, so conky sees the output.

Mahngiel
January 10th, 2010, 11:32 PM
I think you need to look at stdout redirection, so conky sees the output.I'll take a read into this. Thanks for takin the time to respond.

Brandon Williams
January 11th, 2010, 05:18 AM
OK ... I figured it out. The problem is that top requires access to a controlling terminal. When run within conky, it doesn't have that, so top fails. Run top in batch mode (with the -b switch) to get around this.

Mahngiel
January 11th, 2010, 04:45 PM
OK ... I figured it out. The problem is that top requires access to a controlling terminal. When run within conky, it doesn't have that, so top fails. Run top in batch mode (with the -b switch) to get around this.

GENIOUS! Thank you so much for helping me figure this out. this works exactly right.

Kudos.

kaivalagi
January 11th, 2010, 08:39 PM
OK ... I figured it out. The problem is that top requires access to a controlling terminal. When run within conky, it doesn't have that, so top fails. Run top in batch mode (with the -b switch) to get around this.


GENIOUS! Thank you so much for helping me figure this out. this works exactly right.

Kudos.

Excellent, I must admit I had a quick look at redirecting output and still got messed about with what Brandon has highlighted

Good stuff to know...thanks Brandon!