0

I would like to store a dbus output into an array.

dbus-send --system --print-reply --dest="com.ac.comp" /com/ac/comp/IO com.ac.comp.Time.Get

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

method return sender=:1.191 -> dest=:1.198 reply_serial=2
   uint16 1
   uint16 0
   uint16 0
   uint32 0

I have implemented my bash as follow :

if [ -f $file ]                     
then
    IFS=".="                    
    while read enum name ID x                           
    do 
       if [ "$enum" == "IO" ] && [ "$name" == $IOname ]
       then 
       array=($(dbus-send --system --print-reply --dest="com.ac.comp" /com/ac/comp/IO com.ac.comp.IO.Get uint16:16)) 
       fi
    done < $1                       
else
    exit_error 
fi 

I can not explain why the following echo commands :

echo ${array[1]} 
echo
echo ${array[2]} 
echo 
echo ${array[3]} 
echo 
echo ${array[4]} 
echo 
echo ${array[5]}
echo
echo ${array[6]}   
echo
echo ${array[7]}
echo 
echo ${array[8]}   
echo
echo ${array[9]}
echo
echo ${array[10]}  
echo
echo ${array[11]}
echo 
echo ${array[12]}  
echo
echo ${array[13]}

gives

:1

105 -> dest

:1

112 reply_serial

2
   uint16 1
   uint16 0
   uint16 0
   uint32 0

I would like to reuse the uint16 and uint32 values. But I though retrieve them in ${array[7]} ${array[9]} ${array[11]} and ${array[13]}

8
  • What do you get if you pipe the dbus-send output into hexdump or xxd? Commented Jan 9, 2015 at 13:32
  • Sorry Etan, but how can I test these output Commented Jan 9, 2015 at 14:08
  • Hm? I was asking you to tell us what you get if you do that. So that we could see what the shell was seeing when it split the output into the array. Or did you mean how to do that? dbus-send ... | xxd Commented Jan 9, 2015 at 14:12
  • I just edited the main post. This is how the data are stored into the array. It is not a normal behavior .. Commented Jan 9, 2015 at 14:43
  • That is normal if your IFS is set to .. Commented Jan 9, 2015 at 14:44

1 Answer 1

1

You've modified IFS so the shell is splitting on . instead of whitespace.

Don't do that.

read is special in that it can take a "local" value for IFS directly.

So instead of

IFS=.
while read ....; do
    ...
done

which modifies IFS for the entire shell you can do

while IFS=. read ....; do
    ...
done

and only modify IFS for the read built-in.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank Etan. Your solution works fine. I can also use IFS=$' \t\n' to restore the default IFS.
You could, you could also save IFS before you replace it and then restore that value but it is better to not mess with it globally in the first place.

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.