`Interact` not working as expected in bash/expect scripting

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















In order to provision an embedded device (i.e. writing images to flash) a series of very well known commands have to be entered during the boot stage. The algorithm is something like:



  • Capture a bootloader shell by entering any key during boot

  • Flash image A

  • Flash image B

  • ...

  • Continue Boot (with new images)

Since we must first capture the bootloader shell at a certain point in the boot process I figured application of an expect script would solve this problem nicely. I also would like to mix some other bash commands for implementing the logic regarding which device node to connect to, etc. What I came up with (expect portion only) is the following:



#!/bin/bash
DEVICE=...
FLASH_CMD_A=...
#Other bash stuff

expect <<SCRIPT
set timeout 5
spawn plink -serial -sercfg 115200,8,n,1 $DEVICE

expect "Hit any key to stop autoboot"
sleep 1
send "r"

expect "=>"
sleep 1
send "$FLASH_CMD_An"

interact
SCRIPT

echo "Done!"


What I am observing is: The first expect line is properly being captured, the sleep is working, and the newline appears to be being sent. The next expect (capturing the prompt) and its corresponding sleep also appears to be working. The send command ($FLASH_CMD_A) gets printed to the terminal with lots of white spaces between words, but it seems to be working also.



The biggest problem is that the interact does not appear to be doing anything. Once the second expect/send is executed I am dropped back to a bash shell having never even seen "Done!" printed to stdout. I also tried the interact syntax:



interact 
"=>"
send "r"
exp_continue




But that did not work at all. The prompt was never captured and the script just exited immediately as if the interact statement were not there at all. Any ideas on what I may be doing wrong?
I have:



expect -v
expect version 5.45


Although the commands seem to be being passed to the device I cannot be sure. Once I am dropped back to a bash shell all output from the device following those commands is lost. However, if I run my script a second time expect seems to dump its text buffer (expect_out?) and exit immediately. Interestingly, the output I was expecting from the previously sent commands is contained within this buffer dump.



As an aside: I tried this same script inside an VM environment and none of the "expect"ed statements were being captured; all input was printed to stdout and passed through.










share|improve this question
























  • Try to add expect eof after interact

    – Costas
    Jul 11 '16 at 16:44















1















In order to provision an embedded device (i.e. writing images to flash) a series of very well known commands have to be entered during the boot stage. The algorithm is something like:



  • Capture a bootloader shell by entering any key during boot

  • Flash image A

  • Flash image B

  • ...

  • Continue Boot (with new images)

Since we must first capture the bootloader shell at a certain point in the boot process I figured application of an expect script would solve this problem nicely. I also would like to mix some other bash commands for implementing the logic regarding which device node to connect to, etc. What I came up with (expect portion only) is the following:



#!/bin/bash
DEVICE=...
FLASH_CMD_A=...
#Other bash stuff

expect <<SCRIPT
set timeout 5
spawn plink -serial -sercfg 115200,8,n,1 $DEVICE

expect "Hit any key to stop autoboot"
sleep 1
send "r"

expect "=>"
sleep 1
send "$FLASH_CMD_An"

interact
SCRIPT

echo "Done!"


What I am observing is: The first expect line is properly being captured, the sleep is working, and the newline appears to be being sent. The next expect (capturing the prompt) and its corresponding sleep also appears to be working. The send command ($FLASH_CMD_A) gets printed to the terminal with lots of white spaces between words, but it seems to be working also.



The biggest problem is that the interact does not appear to be doing anything. Once the second expect/send is executed I am dropped back to a bash shell having never even seen "Done!" printed to stdout. I also tried the interact syntax:



interact 
"=>"
send "r"
exp_continue




But that did not work at all. The prompt was never captured and the script just exited immediately as if the interact statement were not there at all. Any ideas on what I may be doing wrong?
I have:



expect -v
expect version 5.45


Although the commands seem to be being passed to the device I cannot be sure. Once I am dropped back to a bash shell all output from the device following those commands is lost. However, if I run my script a second time expect seems to dump its text buffer (expect_out?) and exit immediately. Interestingly, the output I was expecting from the previously sent commands is contained within this buffer dump.



As an aside: I tried this same script inside an VM environment and none of the "expect"ed statements were being captured; all input was printed to stdout and passed through.










share|improve this question
























  • Try to add expect eof after interact

    – Costas
    Jul 11 '16 at 16:44













1












1








1








In order to provision an embedded device (i.e. writing images to flash) a series of very well known commands have to be entered during the boot stage. The algorithm is something like:



  • Capture a bootloader shell by entering any key during boot

  • Flash image A

  • Flash image B

  • ...

  • Continue Boot (with new images)

Since we must first capture the bootloader shell at a certain point in the boot process I figured application of an expect script would solve this problem nicely. I also would like to mix some other bash commands for implementing the logic regarding which device node to connect to, etc. What I came up with (expect portion only) is the following:



#!/bin/bash
DEVICE=...
FLASH_CMD_A=...
#Other bash stuff

expect <<SCRIPT
set timeout 5
spawn plink -serial -sercfg 115200,8,n,1 $DEVICE

expect "Hit any key to stop autoboot"
sleep 1
send "r"

expect "=>"
sleep 1
send "$FLASH_CMD_An"

interact
SCRIPT

echo "Done!"


What I am observing is: The first expect line is properly being captured, the sleep is working, and the newline appears to be being sent. The next expect (capturing the prompt) and its corresponding sleep also appears to be working. The send command ($FLASH_CMD_A) gets printed to the terminal with lots of white spaces between words, but it seems to be working also.



The biggest problem is that the interact does not appear to be doing anything. Once the second expect/send is executed I am dropped back to a bash shell having never even seen "Done!" printed to stdout. I also tried the interact syntax:



interact 
"=>"
send "r"
exp_continue




But that did not work at all. The prompt was never captured and the script just exited immediately as if the interact statement were not there at all. Any ideas on what I may be doing wrong?
I have:



expect -v
expect version 5.45


Although the commands seem to be being passed to the device I cannot be sure. Once I am dropped back to a bash shell all output from the device following those commands is lost. However, if I run my script a second time expect seems to dump its text buffer (expect_out?) and exit immediately. Interestingly, the output I was expecting from the previously sent commands is contained within this buffer dump.



As an aside: I tried this same script inside an VM environment and none of the "expect"ed statements were being captured; all input was printed to stdout and passed through.










share|improve this question
















In order to provision an embedded device (i.e. writing images to flash) a series of very well known commands have to be entered during the boot stage. The algorithm is something like:



  • Capture a bootloader shell by entering any key during boot

  • Flash image A

  • Flash image B

  • ...

  • Continue Boot (with new images)

Since we must first capture the bootloader shell at a certain point in the boot process I figured application of an expect script would solve this problem nicely. I also would like to mix some other bash commands for implementing the logic regarding which device node to connect to, etc. What I came up with (expect portion only) is the following:



#!/bin/bash
DEVICE=...
FLASH_CMD_A=...
#Other bash stuff

expect <<SCRIPT
set timeout 5
spawn plink -serial -sercfg 115200,8,n,1 $DEVICE

expect "Hit any key to stop autoboot"
sleep 1
send "r"

expect "=>"
sleep 1
send "$FLASH_CMD_An"

interact
SCRIPT

echo "Done!"


What I am observing is: The first expect line is properly being captured, the sleep is working, and the newline appears to be being sent. The next expect (capturing the prompt) and its corresponding sleep also appears to be working. The send command ($FLASH_CMD_A) gets printed to the terminal with lots of white spaces between words, but it seems to be working also.



The biggest problem is that the interact does not appear to be doing anything. Once the second expect/send is executed I am dropped back to a bash shell having never even seen "Done!" printed to stdout. I also tried the interact syntax:



interact 
"=>"
send "r"
exp_continue




But that did not work at all. The prompt was never captured and the script just exited immediately as if the interact statement were not there at all. Any ideas on what I may be doing wrong?
I have:



expect -v
expect version 5.45


Although the commands seem to be being passed to the device I cannot be sure. Once I am dropped back to a bash shell all output from the device following those commands is lost. However, if I run my script a second time expect seems to dump its text buffer (expect_out?) and exit immediately. Interestingly, the output I was expecting from the previously sent commands is contained within this buffer dump.



As an aside: I tried this same script inside an VM environment and none of the "expect"ed statements were being captured; all input was printed to stdout and passed through.







arch-linux scripting boot-loader expect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '16 at 11:13









Jeff Schaller

44.5k1162143




44.5k1162143










asked Jul 11 '16 at 16:15









sherrellbcsherrellbc

88831530




88831530












  • Try to add expect eof after interact

    – Costas
    Jul 11 '16 at 16:44

















  • Try to add expect eof after interact

    – Costas
    Jul 11 '16 at 16:44
















Try to add expect eof after interact

– Costas
Jul 11 '16 at 16:44





Try to add expect eof after interact

– Costas
Jul 11 '16 at 16:44










1 Answer
1






active

oldest

votes


















0














As a workaround, run the expect script from a file, and not via standard input:



#!/bin/sh

BLAH='echo hi'

ESCRIPT=`mktemp runner.XXXXXXXXXX` || exit 1

# nope
#expect -d - <<SCRIPT
cat >$ESCRIPT <<SCRIPT
set timeout 5
spawn $SHELL
# FIXME whatever your prompt looks like
expect -ex "% "
send -raw "r"
expect -ex "% "
send -- "$BLAHn"
expect -ex "% "
interact
SCRIPT

expect -d $ESCRIPT

echo alas poor $SHELL I knew him well a man of infinit

# and probably also `trap` in the event this code gets whapped with an INT
# or something...
rm $ESCRIPT





share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f295181%2finteract-not-working-as-expected-in-bash-expect-scripting%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As a workaround, run the expect script from a file, and not via standard input:



    #!/bin/sh

    BLAH='echo hi'

    ESCRIPT=`mktemp runner.XXXXXXXXXX` || exit 1

    # nope
    #expect -d - <<SCRIPT
    cat >$ESCRIPT <<SCRIPT
    set timeout 5
    spawn $SHELL
    # FIXME whatever your prompt looks like
    expect -ex "% "
    send -raw "r"
    expect -ex "% "
    send -- "$BLAHn"
    expect -ex "% "
    interact
    SCRIPT

    expect -d $ESCRIPT

    echo alas poor $SHELL I knew him well a man of infinit

    # and probably also `trap` in the event this code gets whapped with an INT
    # or something...
    rm $ESCRIPT





    share|improve this answer



























      0














      As a workaround, run the expect script from a file, and not via standard input:



      #!/bin/sh

      BLAH='echo hi'

      ESCRIPT=`mktemp runner.XXXXXXXXXX` || exit 1

      # nope
      #expect -d - <<SCRIPT
      cat >$ESCRIPT <<SCRIPT
      set timeout 5
      spawn $SHELL
      # FIXME whatever your prompt looks like
      expect -ex "% "
      send -raw "r"
      expect -ex "% "
      send -- "$BLAHn"
      expect -ex "% "
      interact
      SCRIPT

      expect -d $ESCRIPT

      echo alas poor $SHELL I knew him well a man of infinit

      # and probably also `trap` in the event this code gets whapped with an INT
      # or something...
      rm $ESCRIPT





      share|improve this answer

























        0












        0








        0







        As a workaround, run the expect script from a file, and not via standard input:



        #!/bin/sh

        BLAH='echo hi'

        ESCRIPT=`mktemp runner.XXXXXXXXXX` || exit 1

        # nope
        #expect -d - <<SCRIPT
        cat >$ESCRIPT <<SCRIPT
        set timeout 5
        spawn $SHELL
        # FIXME whatever your prompt looks like
        expect -ex "% "
        send -raw "r"
        expect -ex "% "
        send -- "$BLAHn"
        expect -ex "% "
        interact
        SCRIPT

        expect -d $ESCRIPT

        echo alas poor $SHELL I knew him well a man of infinit

        # and probably also `trap` in the event this code gets whapped with an INT
        # or something...
        rm $ESCRIPT





        share|improve this answer













        As a workaround, run the expect script from a file, and not via standard input:



        #!/bin/sh

        BLAH='echo hi'

        ESCRIPT=`mktemp runner.XXXXXXXXXX` || exit 1

        # nope
        #expect -d - <<SCRIPT
        cat >$ESCRIPT <<SCRIPT
        set timeout 5
        spawn $SHELL
        # FIXME whatever your prompt looks like
        expect -ex "% "
        send -raw "r"
        expect -ex "% "
        send -- "$BLAHn"
        expect -ex "% "
        interact
        SCRIPT

        expect -d $ESCRIPT

        echo alas poor $SHELL I knew him well a man of infinit

        # and probably also `trap` in the event this code gets whapped with an INT
        # or something...
        rm $ESCRIPT






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 11 '16 at 18:15









        thrigthrig

        25.3k23257




        25.3k23257



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f295181%2finteract-not-working-as-expected-in-bash-expect-scripting%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay