TCL 期望输出

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

TCL expect output

问题

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

  1. #!/usr/bin/expect -f
  2. log_user 0
  3. set device "34:85:18:6a:52:52"
  4. spawn gatttool -b $device -I
  5. expect "> "
  6. sleep 1
  7. send -- "connect\r"
  8. expect "Connection successful"
  9. sleep 1
  10. set message1 ""
  11. expect {
  12. -re "Notification handle = 0x0033 value: (.+\n)" {
  13. set message1 ${message1}$expect_out(1,string)
  14. exp_continue
  15. }
  16. "^\[.+\]\[LE\]> $" {
  17. send -- "disconnect\r"
  18. }
  19. }
  20. puts "$message1"

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

英文:

small script that should return line values but does not

  1. #!/usr/bin/expect -f
  2. log_user 0
  3. set device "34:85:18:6a:52:52"
  4. spawn gatttool -b $device -I
  5. expect " > "
  6. sleep 1
  7. send -- "connect\r"
  8. expect "Connection successful"
  9. sleep 1
  10. set message1 ""
  11. expect {
  12. -re "Notification handle = 0x0033 value: (.+\n)" {
  13. set message1 ${message1}$expect_out(1,string)
  14. exp_continue
  15. }
  16. "^\[.+\]\[LE\]> $" {
  17. send -- "disconnect\r"
  18. }
  19. }
  20. puts "$message1"

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

  1. mike@raspberrypi:~/Downloads $ gatttool -b 34:85:18:6a:52:52 -I
  2. [34:85:18:6a:52:52][LE]> connect
  3. Attempting to connect to 34:85:18:6a:52:52
  4. Connection successful
  5. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
  6. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
  7. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
  8. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
  9. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
  10. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
  11. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
  12. [34:85:18:6a:52:52][LE]> exit
  13. (gatttool:29107): GLib-WARNING **: 11:37:04.060: Invalid file descriptor.
  14. 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):

  1. export SEXPECT_SOCKFILE=/tmp/gatttool-$$.sock
  2. sexpect spawn -idle 300 gatttool -b 34:85:18:6a:52:52 -I
  3. sexpect expect -exact '[LE]> '
  4. sexpect send -cr connect
  5. message=
  6. pat=$'\[LE\]> |Notification handle = 0x0033 value:([ [:xdigit:]]+)[[:blank:]]*[\r\n]+'
  7. while true; do
  8. sexpect expect -re "$pat" || exit 1
  9. out=$( sexpect expect_out -index 1 )
  10. [[ -n $out ]] && message=$message$out || break
  11. done
  12. sexpect send -cr exit
  13. sexpect wait
  14. echo "---- the message ----"
  15. echo "$message"

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

  1. $ bash gatttool.sh
  2. [34:85:18:6a:52:52][LE]> connect
  3. Attempting to connect to 34:85:18:6a:52:52
  4. Connection successful
  5. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
  6. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
  7. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
  8. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
  9. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
  10. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
  11. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
  12. [34:85:18:6a:52:52][LE]> exit
  13. ---- the message ----
  14. 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):

  1. export SEXPECT_SOCKFILE=/tmp/gatttool-$$.sock
  2. sexpect spawn -idle 300 gatttool -b 34:85:18:6a:52:52 -I
  3. sexpect expect -exact '[LE]> '
  4. sexpect send -cr connect
  5. message=
  6. pat=$'\[LE\]> |Notification handle = 0x0033 value:([ [:xdigit:]]+)[[:blank:]]*[\r\n]+'
  7. while true; do
  8. sexpect expect -re "$pat" || exit 1
  9. out=$( sexpect expect_out -index 1 )
  10. [[ -n $out ]] && message=$message$out || break
  11. done
  12. sexpect send -cr exit
  13. sexpect wait
  14. echo "---- the message ----"
  15. echo "$message"

Test result (using a fake gatttool):

  1. $ bash gatttool.sh
  2. [34:85:18:6a:52:52][LE]> connect
  3. Attempting to connect to 34:85:18:6a:52:52
  4. Connection successful
  5. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
  6. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
  7. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
  8. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
  9. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
  10. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
  11. Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
  12. [34:85:18:6a:52:52][LE]> exit
  13. ---- the message ----
  14. 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:

确定