0

I have a program JLinkExe which opens it's own prompt uponn execution. So I normally run it like this:

JLinkExe

and then type the commands at it's prompt that appears:

J-Link>

There are many applications with it's own prompt and I am interested in a general method that would enable me to send commands to any kind of application that has it's own prompt.


I already tried two methods. They both try to send commands in this order:

  1. connect
  2. Enter
  3. Enter
  4. Enter
  5. Enter
  6. erase
  7. loadbin program.bin , 0x0
  8. r
  9. q

but both fail. Here is the first method:

   { echo 'connect';
     echo '';
     echo '';
     echo '';
     echo '';
     echo 'erase';
     echo 'loadbin program.bin , 0x0';
     echo 'r';
     echo 'q'; } | JLinkExe

And the second method (source):

JLinkExe <<EOF
connect




erase
loadbin program.bin , 0x0
r
q
EOF

I found these method on the internet but I don't understand why they fail. Especially the first one that worked in the past...

Can anyone propose any better / working / universally applicable method?

71GA
  • 1,132
  • 6
  • 36
  • 69

1 Answers1

1

I think it might be because here-docs do not wait for output. Unfortunately for you I switched company, thus can't test my code below.

#! /bin/bash
expect <<-EOF
    set timeout -1
    spawn JLinkExe
    expect "J-Link> " { send "connect\r" }
    expect "J-Link> " { send "\r" }
    expect "J-Link> " { send "\r" }
    expect "J-Link> " { send "\r" }
    expect "J-Link> " { send "\r" }
    expect "J-Link> " { send "erase\r" }
    expect "J-Link> " { send "loadbin program.bin , 0x0\r" }
    expect "J-Link> " { send "r\r" }
    expect "J-Link> " { send "q\r" }
    expect eof
    catch wait result
    exit [lindex \$result 3]
EOF
exit $?

Except waits until J-Link> turns up and then sends the command through the connection.

If it doesn't work please notify me. I'll try to help you after the weekend :-)

EDIT:
A: Why did you wrap everything in expect 2>&1 <<-EOF and EOF?
You can add expect in the shebang, but I often use it as part of my Bash scripts. My knowledge of Bash is better.

B: Why a -EOF instead of EOF?
That's because <<-EOF allows leading tabs when you want to end the here-doc. You can indent it in functions for instance.

C: Why did you redirect stderr to stdout (2>&1)?
In your case I should've removed this. I took the code from one of my other answer about expect and tailored it to your needs.

D: What does catch wait result and exit [lindex \$result 3] do after we catch the eof?
Nice question, I had to look this one up a little myself:

Note that you have to escape the $ in the here-doc, otherwise Bash tries to process it. Hence \$result.

E: Why you exit with exit $?
Bash exits a script with the last known error code. Although you can leave it implicitly, I like to add it anyhow. It keeps the script more readable for beginners.

Bayou
  • 3,293
  • 1
  • 9
  • 22
  • I have a couple of questions. Can you elaboate on the following in your question. **A:** Why did you wrap everything in `expect 2>&1 <<-EOF` and `EOF`? **B:** Why a `-EOF` instead of `EOF`? **C:** Why did you redirect stderr to stdout (`2>&1`)? **D:** What does `catch wait result` and `exit [lindex \$result 3]` do after we catch the `eof`? **E:** Why you exit with `exit $?`? – 71GA Jul 23 '21 at 21:16