Home > Back-end >  shell script Bluetoothctl WRITE
shell script Bluetoothctl WRITE

Time:01-07

#!/usr/bin/expect -f

set prompt "#"

set address 34:85:18:6a:52:52

spawn bluetoothctl

expect -re $prompt

send "connect $address\r"

expect "Connection successful"

sleep 2

send "list-attributes\r"

sleep 2

send "menu gatt\r"

sleep 2

send "select-attribute 
/org/bluez/hci0/dev_34_85_18_6A_52_52/service002e/char002f\r"

sleep 2

send "list-attributes\r"

sleep 2

send "write 0xa5 0x00 0x00 0x08 0x00 0x53 0x6f 0x0e 0x00 0x03 0x10 0x01 0x02 0x12 0x01 0x0a 0xff 0x01 0x20 0x9a\r" 

send "quit\r"

expect eof

i need to be able to have " " quotations around th value being sent or else bluetoothctl write will not accept the value

it works in terminal as

write "0xa5 0x00 0x00 0x08 0x00 0x53 0x6f 0x0e 0x00 0x03 0x10 0x01 0x02 0x12 0x01 0x0a 0xff 0x01 0x20 0x9a"

but script will not allow me to input quotations around value without throwing an error...

ive tred breaking write and value apart and using a \ between them, i really dont have a good grasp of scripting to know how to make it work.

maybe a variable with intended code inside but again i dont have the knowledge to get this completed solo

CodePudding user response:

in bash in general, use \" to echo the ".

e.g.:

$echo "test test is test"
test test is test

$echo "test test is \"test\""
test test is "test"

You can also use single quotes ', e.g.:

echo 'test test is "test"'
test test is "test" 

CodePudding user response:

The first line #!/usr/bin/expect -f shows that this is an Expect script, not a shell script. Expect is based on Tcl, so the Tcl rules for quoting apply. These can be found at http://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm .

The first part of @akathimi's answer is still applicable - backslashes can be used to insert double-quotes in a double-quoted string. However the second part does not apply - single-quotes have no significance in Tcl syntax. However the same effect can be achieved by enclosing the whole string in braces {} like this, e.g. puts {test test is "test"} but note that this will also disable the substitution of a carriage-return character for the sequence \r.

  • Related