簡體   English   中英

Bash將dbus輸出存儲在數組中

[英]Bash store dbus output in array

我想將dbus輸出存儲到數組中。

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

當我執行以上命令時,將獲得以下輸出:

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

我已經實現了我的bash如下:

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 

我無法解釋為什么以下echo命令:

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]}

:1

105 -> dest

:1

112 reply_serial

2
   uint16 1
   uint16 0
   uint16 0
   uint32 0

我想重用uint16和uint32值。 但是我雖然在$ {array [7]} $ {array [9]} $ {array [11]}和$ {array [13]}中檢索它們

您已經修改了IFS以使shell可以拆分. 而不是空格。

不要那樣做

read的特殊之處在於它可以直接為IFS取一個“本地”值。

所以代替

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

可以修改整個外殼的IFS

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

並且僅修改內置read IFS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM