1

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.

5
  • you need not use extra redirection, sed is capable of reading input from file. That is sed -n '1p' $file would work fine Commented Nov 18, 2014 at 13:44
  • Thank ! I just tried your instruction, same result and seven lines removed --' Commented Nov 18, 2014 at 13:51
  • There's no need to pre-cut the file. sed can do that work as well (and in a less-error prone fashion). sed -e '1{s/^ \+(byte|uint16) //;p}' or something roughly like that. Though whether that will be more-or-less efficient depends on whether doing that work in N sed instances is better than doing it in one cut instance N times. Commented Nov 18, 2014 at 13:57
  • You mean that the sed command that you gave could generate the same output ? I tried with the same line and with modification but I obtain only the first line. Thank Commented Nov 18, 2014 at 14:28
  • I'm so sorry Etan but after one hour I am not able to apply your example and thus call dbus-send only one time and store only the byte and uint16 value. Could you give more details ? Commented Nov 18, 2014 at 15:29

1 Answer 1

2
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]}"
.
.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ajaaskel ! It is a very good alternative, I am not obligated to create a file. However, I have to ask a last question. Why did you specify ${array[7]} to retrieve the first byte value ? Within the line "method return sender=:1.0 -> dest=:1.34 reply_serial=2" there is just five term ?
I just counted from your output that you'll have first those extra strings you don't need and the first data might go to index 7 which is cell number 8 in that array since there is word "byte" before the actual value. i.e. 0:method 1:return 2:sender=:1.0 3:-> 4:..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.