You are not logged in.

#1 2009-06-14 19:08:36

Liuuutas
Member
From: Cambridge
Registered: 2009-05-02
Posts: 71

[SOLVED with some issues] Conky and bash scripts

Hi there,

I have several questions about Conky and its capabilities.

Basically I have a tex output from a bashscript which is like this:

|Bat info                      |
|                              |
|Power Drain: 20W              |
|Remaining Capacity: 40Wh      |

(The pipe sign act as a border of conky (bash script does not output these simbols smile )

Well, as I execute this script in conky I get unformatted text which is not what I would like to do...
I wanted to know whether it is possible to execute this script, but align the values of the each property (the string 20W would be aligned to right and "Power Drain" - to left).

I would Like to get something Like this:

|Bat info                      |
|                              |
|Power Drain:               20W|
|Remaining Capacity:       40Wh|

What should I write in the bash script in order to get the desired result?

The bash script outputs text in a way of multi-line cat command.

p.s. sorry for my English... smile

Last edited by Liuuutas (2009-06-15 18:38:18)

Offline

#2 2009-06-14 19:19:33

ntowakbh
Member
Registered: 2009-06-04
Posts: 29

Re: [SOLVED with some issues] Conky and bash scripts

Could you post a copy of the bash script?


Many nights, I lay awake in bed,
Looking up at the stars.
Thinking of life, it's complexity,
And where in the world my ceiling could have gone this time.

Offline

#3 2009-06-14 19:27:34

Liuuutas
Member
From: Cambridge
Registered: 2009-05-02
Posts: 71

Re: [SOLVED with some issues] Conky and bash scripts

The Script:

#!/bin/bash
# Copyright (C) 2007 William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
# This file is licensed under the GPL v2. E-mail me if you have any ideas of
# other licenses to use (BSD, GPL v3, Public Domain, etc.), I'm open :)

# IDEAS
# - make an ncurses interface, and also maybe gtk+ and qt
# - add option to enable modifying the charge control variables

TP_SMAPI_BASE=/sys/devices/platform/smapi

# function to simplify reading from files
# syntax: read_file <variable> <filename> <check_return_value 0/1> <translate 0/1> [[value] [translation_string]]
read_file()
{
  local VARIABLE
  local FILENAME
  local CHECK_RETURN
  local TRANSLATE
  local VAL
  local RET

  VARIABLE="$1"; shift
  FILENAME="$1"; shift
  CHECK_RETURN="$1"; shift
  TRANSLATE="$1"; shift

  VAL=`cat "$FILENAME" 2> /dev/null`
  RET=$?

  if [[ "$CHECK_RETURN" == "1" ]] && [[ $RET -gt 0 ]]
  then
    RESULT="[unavailable]"
  elif [ "$TRANSLATE" == "1" ]
  then
    RESULT="unknown ($RET)"
    while [ -n "$1" ]
    do
      if [ "$VAL" == "$1" ]
      then
        RESULT="$2"
        break
      fi
      shift 2
    done
  else
    RESULT="$VAL"
  fi

  eval "$VARIABLE="'"'"$RESULT"'"'
#  eval "$VARIABLE=$RESULT"
}

give_units()
{
  local VARIABLE
  local UNIT
  local VALUE
  local RESULT

  VARIABLE="$1"
  UNIT="$2"
  eval "VALUE=\$$VARIABLE"

  if (echo "$VALUE" | grep -qs "^-\?[0-9]")
  then
    RESULT="$VALUE $UNIT"
  else
    RESULT="$VALUE"
  fi

  eval "$VARIABLE="'"'"$RESULT"'"'
}

replace_val()
{
  local VARIABLE
  local OLD_VALUE
  local NEW_VALUE
  local VALUE
  local RESULT

  VARIABLE="$1"
  OLD_VALUE="$2"
  NEW_VALUE="$3"

  eval "VALUE=\$$VARIABLE"

  if [ "$VALUE" == "$OLD_VALUE" ]
  then
    RESULT="$NEW_VALUE"
  else
    RESULT="$VALUE"
  fi

  eval "$VARIABLE="'"'"$RESULT"'"'
}

show_help()
{
  cat << EOF

  thinkpad-smapi.sh - Show SMAPI information on ThinkPad laptops, through
                      the exported SMAPI information by the tp_smapi module.

  -a      Show all available information
  -b [n]  Show all available information for battery [n]
  -h      Show this help text
  -m      Show miscellaneous information

EOF
}

show_misc()
{
  cd $TP_SMAPI_BASE

  read_file PCI_POWER_SAVINGS "enable_pci_power_saving_on_boot" 1 1 1 "enabled" 0 "disabled"
  read_file AC_ADAPTER "ac_connected" 1 1 1 "connected" 0 "disconnected"

  cat << EOF
MISCELLANEOUS INFORMATION
=========================

PCI power savings on boot: $PCI_POWER_SAVINGS
AC adapter status: $AC_ADAPTER

EOF
}

show_batt()
{
  SLOT=$1
  cd $TP_SMAPI_BASE/BAT$SLOT

  TMP=`cat installed`
  if [ $TMP -eq 1 ]
  then
    BATT_INSTALLED="yes"
  else
    BATT_INSTALLED="no"
  fi

  BATT_STATE=`cat state`

  if [ $BATT_INSTALLED == "yes" ]
  then

    read_file BARCODING "barcoding" 1 0
    read_file BATT_CHEMISTRY "chemistry" 1 0
    read_file DESIGN_CAPACITY "design_capacity" 1 0
    read_file DESIGN_VOLTAGE "design_voltage" 1 0
    read_file MANUFACTURE_DATE "manufacture_date" 1 0
    read_file BATT_FRU "model" 1 0
    read_file MANUFACTURER "manufacturer" 1 0
    read_file BATT_SERIAL "serial" 1 0

    read_file FIRST_USE_DATE "first_use_date" 1 0
    read_file CYCLE_COUNT "cycle_count" 1 0
    read_file LAST_FULL_CAPACITY "last_full_capacity" 1 0
    read_file CURRENT_1MIN "current_avg" 1 0
    read_file POWER_1MIN "power_avg" 1 0

    read_file CAPACITY_MWH "remaining_capacity" 1 0
    read_file CAPACITY_PERCENT "remaining_percent" 1 0
    read_file REMAINING_RUNNING_TIME "remaining_running_time" 1 0
    read_file CURRENT_NOW "current_now" 1 0
    read_file POWER_NOW "power_now" 1 0
    read_file BATT_TEMPERATURE "temperature" 1 0
    read_file BATT_VOLTAGE "voltage" 1 0
    read_file REMAINING_CHARGING_TIME "remaining_charging_time" 1 0

    # fixups
    give_units DESIGN_CAPACITY "mWh"
    give_units DESIGN_VOLTAGE "mV"
    give_units CURRENT_NOW "mA"
    give_units POWER_NOW "mW"
    give_units CURRENT_1MIN "mA"
    give_units POWER_1MIN "mW"
    give_units LAST_FULL_CAPACITY "mWh"
    give_units REMAINING_RUNNING_TIME "min"
    give_units REMAINING_CHARGING_TIME "min"
    give_units CAPACITY_PERCENT "%"
    give_units BATT_TEMPERATURE "mC"
    give_units BATT_VOLTAGE "mV"

    # some more
    replace_val REMAINING_RUNNING_TIME "not_discharging" "[not discharging]"
    replace_val REMAINING_CHARGING_TIME "not_charging" "[not charging]"

  else

    BATT_FRU=
    MANUFACTURER=   
    BATT_SERIAL=
    BARCODING=
    BATT_CHEMISTRY=
    DESIGN_CAPACITY=
    DESIGN_VOLTAGE=
    MANUFACTURE_DATE=

    FIRST_USE_DATE=
    CYCLE_COUNT=
    LAST_FULL_CAPACITY=
    CURRENT_1MIN=
    POWER_1MIN=

    CAPACITY_MWH=
    CAPACITY_PERCENT=
    REMAINING_RUNNING_TIME=
    CURRENT_NOW=
    POWER_NOW=
    BATT_TEMPERATURE=
    BATT_VOLTAGE=
    REMAINING_CHARGING_TIME=

  fi

  read_file START_CHARGE_THRESH "start_charge_thresh" 1 0
  read_file STOP_CHARGE_THRESH "stop_charge_thresh" 1 0
  read_file INHIBIT_CHARGE_MIN "inhibit_charge_minutes" 1 0
  read_file BATT_FORCE_DISCHARGE "force_discharge" 1 1 1 "yes" 0 "no"

# installed*, state*, dump
# barcoding, chemistry, design_capacity, design_voltage, manufacture_date, model, manufacturer, serial
# first_use_date, cycle_count, last_full_capacity, current_avg, power_avg
# remaining_capacity, remaining_percent, remaining_running_time, current_now, power_now, temperature, voltage, remaining_charging_time
# start_charge_thresh*, stop_charge_thresh*, inhibit_charge_minutes*, force_discharge*

  cat << EOF
BATTERY $SLOT INFORMATION
=====================

Battery slot:
  Battery present: $BATT_INSTALLED
  Battery state: $BATT_STATE

EOF

  if [ "$BATT_INSTALLED" == "yes" ]
  then
    cat << EOF
Embedded info:
  FRU P/N: $BATT_FRU
  Barcoding: $BARCODING
  Serial number: $BATT_SERIAL
  OEM Manufacturer: $MANUFACTURER
  Chemistry: $BATT_CHEMISTRY
  Manufacture date: $MANUFACTURE_DATE
  Design capacity & voltage: $DESIGN_CAPACITY, $DESIGN_VOLTAGE

Battery health:
  First use date: $FIRST_USE_DATE
  Cycle count: $CYCLE_COUNT
  Last full capacity: $LAST_FULL_CAPACITY
  Average current / power (past 1 minute): $CURRENT_1MIN, $POWER_1MIN

Battery status:
  Remaining capacity: $CAPACITY_MWH mWh ($CAPACITY_PERCENT)
  Remaining running time: $REMAINING_RUNNING_TIME
  Running current & power: $CURRENT_NOW, $POWER_NOW
  Temperature: $BATT_TEMPERATURE
  Voltage: $BATT_VOLTAGE
  Remaining charging time: $REMAINING_CHARGING_TIME

EOF
  fi

  cat << EOF
Battery charging control:
  Start charging at: $START_CHARGE_THRESH %
  Stop charging at: $STOP_CHARGE_THRESH %
  Prevent charging for: $INHIBIT_CHARGE_MIN min
  Force battery discharge: $BATT_FORCE_DISCHARGE

EOF
}

SHOW_ALL=
SHOW_BATT=
SHOW_MISC=
GOT_OPTS=

while getopts ab:hm OPT
do
  GOT_OPTS=y
  case ${OPT} in
  a) SHOW_ALL=y;;
  b) if [ ${OPTARG} -eq 0 -o ${OPTARG} -eq 1 ]
     then
       SHOW_BATT=${OPTARG}
     else
       SHOW_BATT=y
     fi
     ;;
  h) show_help
     exit 0
     ;;
  m) SHOW_MISC=y;;
  \?) show_help
     exit 1
     ;;
  esac
done

if [ "$GOT_OPTS" != y ]
then
  show_help
  exit 1
fi

if [ "$SHOW_ALL" == y ]
then
  show_misc
  show_batt 0
  show_batt 1
else
  if [ -n "$SHOW_MISC" ]
  then
    show_misc
  fi
  if [ -n "$SHOW_BATT" ]
  then
    show_batt $SHOW_BATT
  fi
fi

from ThinkWiki

Last edited by Liuuutas (2009-06-14 19:28:26)

Offline

#4 2009-06-14 19:41:39

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED with some issues] Conky and bash scripts

Can you run it through sed? You just have to move some spaces:

script.sh | sed 's/: \([^ ]*\)\( *\)/: \2\1/'

Offline

#5 2009-06-14 19:48:24

Liuuutas
Member
From: Cambridge
Registered: 2009-05-02
Posts: 71

Re: [SOLVED with some issues] Conky and bash scripts

Well, it did not help. I got almost the output, if not the same.

Offline

#6 2009-06-14 19:51:10

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED with some issues] Conky and bash scripts

@Liuutas: Post the output. I used just echo on the above example and it did work.

Offline

#7 2009-06-14 19:58:16

Liuuutas
Member
From: Cambridge
Registered: 2009-05-02
Posts: 71

Re: [SOLVED with some issues] Conky and bash scripts

22:44:05 | ignas@chemlin   ~ 
$ ~/.scripts/./thinkpad-smapi.sh -a | sed 's/: \([^ ]*\)\( *\)/: \2\1/'
MISCELLANEOUS INFORMATION
=========================

PCI power savings on boot: [unavailable]
AC adapter status: connected

BATTERY 0 INFORMATION

Battery slot:
  Battery present: ${alignr}yes
  Battery state: idle

Embedded info:
  FRU P/N: IBM-COMPATIBLE
  Barcoding: 1Z7SS33CYIP
  Serial number: 10000
  OEM Manufacturer: TN
  Chemistry: LION
  Manufacture date: 2007-07-01
  Design capacity & voltage:  71280mWh, 10800 mV

Battery health:
  First use date: 1980-00-00
  Cycle count: 78
  Last full capacity:  46480mWh
  Average current / power (past 1 minute):  0mA, 0 mW

Battery status:
  Remaining capacity:  27390mWh (59 %)
  Remaining running time:  [notdischarging]
  Running current & power:  0mA, 0 mW
  Temperature:  27400mC
  Voltage:  11684mV
  Remaining charging time:  [notcharging]

Battery charging control:
  Start charging at:  40%
  Stop charging at:  80%
  Prevent charging for:  0min
  Force battery discharge: no

BATTERY 1 INFORMATION

Battery slot:
  Battery present: ${alignr}no
  Battery state: none

Battery charging control:
  Start charging at:  96%
  Stop charging at:  100%
  Prevent charging for:  0min
  Force battery discharge: no

and conky window is of the same text alignment

Could you paste your output? Maybe I'm doing something wrong...

Last edited by Liuuutas (2009-06-14 20:02:31)

Offline

#8 2009-06-14 20:23:53

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED with some issues] Conky and bash scripts

Ah now I see what you meant with the boxes.

Try this instead, it will it print the second part with variable width, depending on the length of the first part. Try different values for 50. But if it's too short then on long lines it will give negative values to printf making it print without formatting.

... | awk -F: '{ len=50-length($1); if ( $1 == "" ) { print } else {printf "%s: %*.*s\n", $1, len, len, $2}}'

Offline

#9 2009-06-15 04:54:52

Liuuutas
Member
From: Cambridge
Registered: 2009-05-02
Posts: 71

Re: [SOLVED with some issues] Conky and bash scripts

Thank you very much... smile It worked.... However, it works only with monospace fonts.... smile But it's not a real problem.

Now, for curiosity:
I have read, that conky is able to handle LUA scripts... Should I look there? Would it be possible to achieve the same result with that, but only using Arial font?

By the way:
I thought that it is also possible to get the desired effect with the conky smapi command, BUT why I chose to use bash scripts was, that conky wasn't able to divide big values of numbers by 1000 ( 46.7 Wh would be better indeed than 46690 mWh )...

Last edited by Liuuutas (2009-06-15 19:29:14)

Offline

#10 2009-06-18 16:25:40

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED with some issues] Conky and bash scripts

I am not too familiar with conky, but I see an ${alignr} in there, does that mean you can use

${alignl}Item description: ${alignr}Value

If so maybe you can use just sed and it will work with variable width fonts
... | sed 's/^/${alignl}/;s/: /&${alignr}/'

Offline

#11 2009-06-18 17:42:02

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: [SOLVED with some issues] Conky and bash scripts

you should take a look at execp


http://conky.sourceforge.net/variables.html


execp parses conky varables so you could add ${color} ${alignr} to the output of your bash script and conky will use the formatting


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

Board footer

Powered by FluxBB