我試圖創建Bash一個JSON兼容輸出,可通過的NodeJS讀取&蟒蛇:bash的輸出轉換爲JSON /詞典
{"link":XX,"signal":YY,"noise":ZZ}
這裏的未經過濾的結果:
iwconfig wlan0
wlan0 IEEE 802.11bg ESSID:"[email protected]" Nickname:"<[email protected]>"
Mode:Managed Frequency:2.452 GHz Access Point: C8:4C:75:20:B4:8E
Bit Rate:54 Mb/s Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Encryption key:A022-1191-3A Security mode:open
Power Management:off
Link Quality=100/100 Signal level=67/100 Noise level=0/100
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
但應用我的過濾器後:
iwconfig wlan0 | grep Link | tr -d '/100' | tr '=' ' ' | xargs | awk '{printf "{\"link\":"$3",\"signal\":"$6",\"noise\":"$9"}"}'
我越來越不穩定和不完整的結果:
{"link":98,"signal":6,"noise":}
{"link":Signal,"signal":Noise,"noise":}
「噪聲」值永遠不會被捕獲,有時printf返回錯誤的塊。
有沒有更可靠的方法來做到這一點?
感謝所有的建議,但我真的需要修復我的代碼版本,因爲我最終與REGEX和所選的@Thrustmaster解決方案真正的鬥爭。 – crankshaft