TCL 期望输出

huangapple go评论62阅读模式
英文:

TCL expect output

问题

以下是您要翻译的代码部分:

#!/usr/bin/expect -f
log_user 0

set device "34:85:18:6a:52:52"

spawn gatttool -b $device -I
expect "> "
sleep 1
send -- "connect\r"
expect "Connection successful"
sleep 1
set message1 ""

expect {
    -re "Notification handle = 0x0033 value: (.+\n)" {
        set message1 ${message1}$expect_out(1,string)
        exp_continue
    }
    "^\[.+\]\[LE\]> $" {
        send -- "disconnect\r"
    }
}

puts "$message1"

请注意,代码中的特殊字符已经被解码,并且代码部分已经被翻译。

英文:

small script that should return line values but does not

#!/usr/bin/expect -f
log_user 0

set device "34:85:18:6a:52:52"

spawn gatttool -b $device -I
expect " > "
sleep 1
send -- "connect\r"
expect "Connection successful"
sleep 1
set message1 ""

expect {
    -re "Notification handle = 0x0033 value: (.+\n)" {
        set message1 ${message1}$expect_out(1,string)
        exp_continue
    }
    "^\[.+\]\[LE\]> $" {
        send -- "disconnect\r"
    }
}

puts "$message1"

the output im trying o save looks li this from normal gatt to address

mike@raspberrypi:~/Downloads $ gatttool -b 34:85:18:6a:52:52 -I 
[34:85:18:6a:52:52][LE]> connect
Attempting to connect to 34:85:18:6a:52:52
Connection successful
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c 
[34:85:18:6a:52:52][LE]> exit

(gatttool:29107): GLib-WARNING **: 11:37:04.060: Invalid file descriptor.
mike@raspberrypi:~/Downloads $ 

tried pipe'n output it returns empty file, would really just like to return a value and tried awk but for some reason i dont think it working correctly to return the values

答案1

得分: 1

这是一个使用我的 sexpect 的简单示例。你可以参考这个示例来更新你的 Expect 脚本。

脚本 (gatttool.sh):

export SEXPECT_SOCKFILE=/tmp/gatttool-$$.sock

sexpect spawn -idle 300 gatttool -b 34:85:18:6a:52:52 -I
sexpect expect -exact '[LE]> '
sexpect send -cr connect

message=
pat=$'\[LE\]> |Notification handle = 0x0033 value:([ [:xdigit:]]+)[[:blank:]]*[\r\n]+'
while true; do
    sexpect expect -re "$pat" || exit 1
    out=$( sexpect expect_out -index 1 )
    [[ -n $out ]] && message=$message$out || break
done

sexpect send -cr exit
sexpect wait

echo "---- the message ----"
echo "$message"

测试结果(使用假的 gatttool):

$ bash gatttool.sh
[34:85:18:6a:52:52][LE]> connect
Attempting to connect to 34:85:18:6a:52:52
Connection successful
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
[34:85:18:6a:52:52][LE]> exit
---- the message ----
 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
英文:

Here's a simple example using my sexpect. You can get the idea and update your Expect script accordingly.

The script (gatttool.sh):

export SEXPECT_SOCKFILE=/tmp/gatttool-$$.sock

sexpect spawn -idle 300 gatttool -b 34:85:18:6a:52:52 -I
sexpect expect -exact '[LE]> '
sexpect send -cr connect

message=
pat=$'\[LE\]> |Notification handle = 0x0033 value:([ [:xdigit:]]+)[[:blank:]]*[\r\n]+'
while true; do
    sexpect expect -re "$pat" || exit 1
    out=$( sexpect expect_out -index 1 )
    [[ -n $out ]] && message=$message$out || break
done

sexpect send -cr exit
sexpect wait

echo "---- the message ----"
echo "$message"

Test result (using a fake gatttool):

$ bash gatttool.sh
[34:85:18:6a:52:52][LE]> connect
Attempting to connect to 34:85:18:6a:52:52
Connection successful
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
[34:85:18:6a:52:52][LE]> exit
---- the message ----
 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c

huangapple
  • 本文由 发表于 2023年1月9日 00:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定