Score:0

Save name and MAC ADRESS of all visible bluetooth device to array

us flag

I want to save name and mac adress of all bluetooth device to two arrays after run this command bt-device -l: 1

The result should like this :

 NAME=["Device1","Device2"]
 MAC_ADDRESS=["Mac_address1" , "Mac_address2"]

Please help me! I'm using ubuntu 20.04 , working with bash script.

Pilot6 avatar
cn flag
Please don't post pictures of text.
user535733 avatar
cn flag
This resembles a homework problem.
thinh2k1310 avatar
us flag
I'm going to build a device controller. I'm learning bash script myself. It's not a homework.
Score:0
cn flag

Since you used picture I had to create a file to store your output

$ cat btdev 
Thinh (38:89:2C:4D:CF:B3)
LG-PN1(CB) (AC:B1:EE:35:AB:CB)

We now need to separate the columns, then read each column into an array. These commands create two files each containing one of the columns:

$ cat btdev | tr ' ' '\t' | cut -f1 > devices
$ cat btdev | tr ' ' '\t' | cut -f2 > macs

(For field operation, the cut command expects TABs and not spaces)

Now we can populate the arrays:

$ readarray -t NAME <  devices 
$ readarray -t MAC-ADDRESS <  macs

Check the arrays:

$ echo ${NAME[0]}
Thinh
$ echo ${NAME[1]}
LG-PN1(CB)
$ echo ${MAC_ADDRESS[0]}
(38:89:2C:4D:CF:B3)
$ echo ${MAC_ADDRESS[1]}
(AC:B1:EE:35:AB:CB)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.