Expect: How to access variables and commands












0















I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.



/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}

send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}


The output shows that it hasnt got the value for item.



]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such 
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"









share|improve this question























  • Why did you escape the $ before running ls and not for item?

    – Inian
    Nov 20 '18 at 13:20











  • If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

    – Gill
    Nov 20 '18 at 13:29











  • take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 20 '18 at 14:07
















0















I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.



/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}

send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}


The output shows that it hasnt got the value for item.



]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such 
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"









share|improve this question























  • Why did you escape the $ before running ls and not for item?

    – Inian
    Nov 20 '18 at 13:20











  • If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

    – Gill
    Nov 20 '18 at 13:29











  • take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 20 '18 at 14:07














0












0








0








I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.



/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}

send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}


The output shows that it hasnt got the value for item.



]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such 
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"









share|improve this question














I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.



/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}

send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}


The output shows that it hasnt got the value for item.



]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such 
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"






bash expect spawn






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 13:17









GillGill

64




64













  • Why did you escape the $ before running ls and not for item?

    – Inian
    Nov 20 '18 at 13:20











  • If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

    – Gill
    Nov 20 '18 at 13:29











  • take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 20 '18 at 14:07



















  • Why did you escape the $ before running ls and not for item?

    – Inian
    Nov 20 '18 at 13:20











  • If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

    – Gill
    Nov 20 '18 at 13:29











  • take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 20 '18 at 14:07

















Why did you escape the $ before running ls and not for item?

– Inian
Nov 20 '18 at 13:20





Why did you escape the $ before running ls and not for item?

– Inian
Nov 20 '18 at 13:20













If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

– Gill
Nov 20 '18 at 13:29





If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"

– Gill
Nov 20 '18 at 13:29













take a look at sexpect with which you can write Expect scripts with shell code only.

– pynexj
Nov 20 '18 at 14:07





take a look at sexpect with which you can write Expect scripts with shell code only.

– pynexj
Nov 20 '18 at 14:07












1 Answer
1






active

oldest

votes


















0














From the expect side:




  • single quotes have no special meaning in expect

  • use braces for non-interpolating quoting

  • however the r sequence will not be interpreted in braces


So you get



send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"


From the shell side




  • don't parse ls

  • don't use for to iterate lines


You want



send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53393877%2fexpect-how-to-access-variables-and-commands%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














    From the expect side:




    • single quotes have no special meaning in expect

    • use braces for non-interpolating quoting

    • however the r sequence will not be interpreted in braces


    So you get



    send {for item in $(ls); do sort -k1 -n "$item"; done}
    send "r"


    From the shell side




    • don't parse ls

    • don't use for to iterate lines


    You want



    send "printf '%sn' * | sort -k1 -nr"
    # or
    send "ls -1vr"





    share|improve this answer




























      0














      From the expect side:




      • single quotes have no special meaning in expect

      • use braces for non-interpolating quoting

      • however the r sequence will not be interpreted in braces


      So you get



      send {for item in $(ls); do sort -k1 -n "$item"; done}
      send "r"


      From the shell side




      • don't parse ls

      • don't use for to iterate lines


      You want



      send "printf '%sn' * | sort -k1 -nr"
      # or
      send "ls -1vr"





      share|improve this answer


























        0












        0








        0







        From the expect side:




        • single quotes have no special meaning in expect

        • use braces for non-interpolating quoting

        • however the r sequence will not be interpreted in braces


        So you get



        send {for item in $(ls); do sort -k1 -n "$item"; done}
        send "r"


        From the shell side




        • don't parse ls

        • don't use for to iterate lines


        You want



        send "printf '%sn' * | sort -k1 -nr"
        # or
        send "ls -1vr"





        share|improve this answer













        From the expect side:




        • single quotes have no special meaning in expect

        • use braces for non-interpolating quoting

        • however the r sequence will not be interpreted in braces


        So you get



        send {for item in $(ls); do sort -k1 -n "$item"; done}
        send "r"


        From the shell side




        • don't parse ls

        • don't use for to iterate lines


        You want



        send "printf '%sn' * | sort -k1 -nr"
        # or
        send "ls -1vr"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 14:14









        glenn jackmanglenn jackman

        168k26147239




        168k26147239
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53393877%2fexpect-how-to-access-variables-and-commands%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()