Linux 在tcl/expect中解析多行

Linux 在tcl/expect中解析多行,linux,shell,bluetooth,raspberry-pi,expect,Linux,Shell,Bluetooth,Raspberry Pi,Expect,我正在做一个expect脚本,它执行bluetoothctl来读取周围的一些ble设备 为此,我编写了下一个脚本: #!/usr/bin/expect send_user "\nStarting scanning\n\r" spawn bluetoothctl expect -re "#" expect "Agent registered" send_user "\nConfiguring scan filter\n\r&qu

我正在做一个expect脚本,它执行bluetoothctl来读取周围的一些ble设备

为此,我编写了下一个脚本:

#!/usr/bin/expect
send_user "\nStarting scanning\n\r"
spawn bluetoothctl
expect -re "#"
expect "Agent registered"
send_user "\nConfiguring scan filter\n\r"
send "menu scan\r"
expect -re "#"
send "uuids 0x1802\r"
expect -re "#"
send "back\r"
expect -re "#"
send_user "\nScanning devices\n\r"
send "scan on\r"
sleep 10
send "devices\r"
expect -re {Device ([0-9A-F:]+)*} {
    expect -re "#"
    set devices $expect_out(1,string)
}
puts "Devices = $devices"
foreach device $devices {
    send "info $device\r"
    expect -re {UUID: Vendor specific           ([0-9A-F-]+)*} {
        expect -re "#"
        set uuid $expect_out(1,string)
    }
    puts "UUID obtained: $uuid"
}
当我执行它时,我有两个错误:

错误1-返回多行。如果send“devices\r”命令返回2行或更多行,我只能读取第一行返回的内容,但我必须保存所有内容:

[bluetooth]# devices
Device 4F:BF:9A:F6:77:32 4F-BF-9A-F6-77-32
Device 04:D1:3A:D7:71:5E Redmi
[bluetooth]# Devices = 4F:BF:9A:F6:77:32
错误2-在foreach中(对于获得的每个设备),我需要解析de info命令输出,以获取行“UUID:特定于供应商”的UUID:

我只需要获得下一个字符串“0A4DADB-66d6-42b7-8d01-bc43b618b6aa”,但我无法找到解决方案

我必须将所有UUID(对于扫描中获得的所有设备)保存到一个数组中,以将它们返回到父脚本

有人能帮我解决这个问题吗


谢谢大家!

我不相信
bluetoothctl
是以这种方式使用的。BlueZ开发人员总是在更改bluetoothctl,如果您的脚本正常工作,它将破坏您的脚本

更好的方法是使用BlueZ提供的D-BusAPI

要执行此操作,您需要知道:

  • BlueZ服务是“org.BlueZ”
  • 适配器设备的D总线对象路径通常为“/org/bluez/hci0”
  • 适配器的DBus接口是“org.bluez.Adapter1”

  • 那么,您对使用BluezAPI的建议是什么?从Java使用?来自壳牌?非常感谢。大多数编程语言都有到D-Bus的可用绑定。这里列出了一些:。我个人使用Python和pydbus,直接用于许多任务。对于命令行,我喜欢
    busctl
    。方法调用示例:
    busctl call org.bluez/org/bluez/hci0 org.bluez.Adapter1 StartDiscovery
    [bluetooth]# info 4F:BF:9A:F6:77:32
    Device 4F:BF:9A:F6:77:32 (random)
        Alias: 4F-BF-9A-F6-77-32
        Paired: no
        Trusted: no
        Blocked: no
        Connected: no
        LegacyPairing: no
        UUID: Immediate Alert           (00001802-0000-1000-8000-00805f9b34fb)
        UUID: Vendor specific           (0a4dadab-66d6-42b7-8d01-bc43b618b6aa)