Search code examples
bashshelldbus

bash script retrieve dbus output


Today, I have to retrieve the result of dbus command.

dbus-send --session --print-reply --dest="com.ac.comp" /com/ac/comp/Time com.ac.comp.Time.GetTime

When I execute the above command, I obtain the following output :

method return sender=:1.0 -> dest=:1.34 reply_serial=2
   byte 0
   byte 0
   byte 0
   byte 0
   byte 0
   byte 0
   uint16 0

Values are given according to the following order :

second
minute
hour
weekday
dateday
month
year

I have implemented my bash as follow :

dbus-send --session --print-reply --dest="com.ac.comp" /com/ac/comp/Time com.ac.comp.Time.GetTime | grep -E byte | cut -c 9-11 > "$file" 
dbus-send --session --print-reply --dest="com.ac.comp" /com/ac/comp/Time com.ac.comp.Time.GetTime | grep -E uint16 | cut -c 11-13 >> "$file" 

second=$(sed -n '1p' < $file)
minute=$(sed -n '2p' < $file)
hour=$(sed -n '3p' < $file)
weekday=$(sed -n '4p' < $file)
dateday=$(sed -n '5p' < $file)
month=$(sed -n '6p' < $file)
year=$(sed -n '7p' < $file) 
echo -e "Time ==> Day : $weekday - Date : $dateday/$month/$year - Time : $hour:$minute:$second"  

The job is done correctly. However, I am sure that my script could be maximized and dbus has not to be called twice and there is a way to do all stuff in less lines.

As new bash programmer, I need advices on this issue and to learn new methods.


Solution

  • array=($(dbus-send --session --print-reply --dest="com.ac.comp" /com/ac/comp/Time com.ac.comp.Time.GetTime))
    

    Maybe somewhere here what you are after:

    echo "Seconds = ${array[7]}"
    echo "Minutes = ${array[9]}"
    .
    .