how to continue bash script execution when expect part is finished

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












1














I have bash script and inside it part with expect code:



#!/bin/bash
set -ex

funct()

pass3="some_pass"

expect -c "
spawn su - oracle
expect "Password:"
send "$pass3n"
interact
"

funct

var_ulimit=`ulimit -Ha |grep "open files" |awk 'print $4'`
echo $var_ulimit


when i execute the script ./script.sh, part with exepct work's ok i have switched to correct user. Problem is that script stop's at that point when executed from shell. Only when i do exit from shell bash script continues. Is it possible to tune expect part of the script to proceed with the rest of the bash script and to preserve shell for the rest of the script when do su - oracle?



Thnx










share|improve this question























  • I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
    – glenn jackman
    May 15 '15 at 12:44










  • goal is to execute commands after expect part withinin the shell invoked with su - oracle
    – klerk
    May 15 '15 at 12:56










  • Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
    – glenn jackman
    May 15 '15 at 13:36















1














I have bash script and inside it part with expect code:



#!/bin/bash
set -ex

funct()

pass3="some_pass"

expect -c "
spawn su - oracle
expect "Password:"
send "$pass3n"
interact
"

funct

var_ulimit=`ulimit -Ha |grep "open files" |awk 'print $4'`
echo $var_ulimit


when i execute the script ./script.sh, part with exepct work's ok i have switched to correct user. Problem is that script stop's at that point when executed from shell. Only when i do exit from shell bash script continues. Is it possible to tune expect part of the script to proceed with the rest of the bash script and to preserve shell for the rest of the script when do su - oracle?



Thnx










share|improve this question























  • I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
    – glenn jackman
    May 15 '15 at 12:44










  • goal is to execute commands after expect part withinin the shell invoked with su - oracle
    – klerk
    May 15 '15 at 12:56










  • Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
    – glenn jackman
    May 15 '15 at 13:36













1












1








1







I have bash script and inside it part with expect code:



#!/bin/bash
set -ex

funct()

pass3="some_pass"

expect -c "
spawn su - oracle
expect "Password:"
send "$pass3n"
interact
"

funct

var_ulimit=`ulimit -Ha |grep "open files" |awk 'print $4'`
echo $var_ulimit


when i execute the script ./script.sh, part with exepct work's ok i have switched to correct user. Problem is that script stop's at that point when executed from shell. Only when i do exit from shell bash script continues. Is it possible to tune expect part of the script to proceed with the rest of the bash script and to preserve shell for the rest of the script when do su - oracle?



Thnx










share|improve this question















I have bash script and inside it part with expect code:



#!/bin/bash
set -ex

funct()

pass3="some_pass"

expect -c "
spawn su - oracle
expect "Password:"
send "$pass3n"
interact
"

funct

var_ulimit=`ulimit -Ha |grep "open files" |awk 'print $4'`
echo $var_ulimit


when i execute the script ./script.sh, part with exepct work's ok i have switched to correct user. Problem is that script stop's at that point when executed from shell. Only when i do exit from shell bash script continues. Is it possible to tune expect part of the script to proceed with the rest of the bash script and to preserve shell for the rest of the script when do su - oracle?



Thnx







bash expect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 15 '15 at 10:38

























asked May 15 '15 at 10:32









klerk

1,4141913




1,4141913











  • I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
    – glenn jackman
    May 15 '15 at 12:44










  • goal is to execute commands after expect part withinin the shell invoked with su - oracle
    – klerk
    May 15 '15 at 12:56










  • Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
    – glenn jackman
    May 15 '15 at 13:36
















  • I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
    – glenn jackman
    May 15 '15 at 12:44










  • goal is to execute commands after expect part withinin the shell invoked with su - oracle
    – klerk
    May 15 '15 at 12:56










  • Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
    – glenn jackman
    May 15 '15 at 13:36















I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
– glenn jackman
May 15 '15 at 12:44




I'm not clear on why you are doing su - oracle and then doing nothing in that shell. What is your goal here?
– glenn jackman
May 15 '15 at 12:44












goal is to execute commands after expect part withinin the shell invoked with su - oracle
– klerk
May 15 '15 at 12:56




goal is to execute commands after expect part withinin the shell invoked with su - oracle
– klerk
May 15 '15 at 12:56












Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
– glenn jackman
May 15 '15 at 13:36




Then you have no choice but to expand your expect script. When your funct function returns, the "su" shell has been closed. I'll throw some ideas into an answer
– glenn jackman
May 15 '15 at 13:36










3 Answers
3






active

oldest

votes


















1














The last two commands will never be executed in the shell of the oracle user. Try that:



expect -c "
log_user 0
spawn su - oracle -c "ulimit -Hn"
expect "Passwort:"
send "$pass3n"
expect eof
puts "$expect_out(buffer)"
"


Also assuming that you only want the open files limit, with the -n flag (no need to grep, awk, ...).






share|improve this answer






















  • it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
    – klerk
    May 15 '15 at 12:23










  • @klerk You can call the command within expect itself. send "your_command"...
    – chaos
    May 15 '15 at 12:28


















1














This is largely untested. The aim is to send the commands you want to run as user "oracle" to the function as a parameter, and then get those to execute within the expect session.



This uses single-quoted here-docs to contain both the commands you want to run as oracle and the expect body, so take care to match the quoting I'm demonstrating here.



#!/bin/bash
set -ex

as_oracle()
local pass="the_password"
local cmds=$(cat -) # read the oracle commands from stdin

# quoting in the next line is very important
expect -f - "$pass" "$cmds" <<'END_EXPECT'
lassign $argv pass3 commands

spawn su - oracle
expect "Password:"
send -- "$pass3r"

# this is the regular expression that matches the end
# of oracle's prompt: please change it if it's not correct
set prompt $ $

expect -re $prompt
send -- "eval "$commands"r"
expect -re $prompt
send -- "exitr"
expect eof
END_EXPECT



as_oracle <<'END_ORACLE'
var_ulimit=$(ulimit -Ha | awk '/open files/ print $4')
echo $var_ulimit
END_ORACLE


I'm not certain that eval is the way to execute those commands. This may be better:



send -- ". <(echo "$commands")r"





share|improve this answer




























    0














    Create a new script file for your expect script like "expect.sh".
    And call it in your bash script:



    ./expect.sh


    Now your expect script will work in your bash script.






    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%2f203594%2fhow-to-continue-bash-script-execution-when-expect-part-is-finished%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The last two commands will never be executed in the shell of the oracle user. Try that:



      expect -c "
      log_user 0
      spawn su - oracle -c "ulimit -Hn"
      expect "Passwort:"
      send "$pass3n"
      expect eof
      puts "$expect_out(buffer)"
      "


      Also assuming that you only want the open files limit, with the -n flag (no need to grep, awk, ...).






      share|improve this answer






















      • it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
        – klerk
        May 15 '15 at 12:23










      • @klerk You can call the command within expect itself. send "your_command"...
        – chaos
        May 15 '15 at 12:28















      1














      The last two commands will never be executed in the shell of the oracle user. Try that:



      expect -c "
      log_user 0
      spawn su - oracle -c "ulimit -Hn"
      expect "Passwort:"
      send "$pass3n"
      expect eof
      puts "$expect_out(buffer)"
      "


      Also assuming that you only want the open files limit, with the -n flag (no need to grep, awk, ...).






      share|improve this answer






















      • it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
        – klerk
        May 15 '15 at 12:23










      • @klerk You can call the command within expect itself. send "your_command"...
        – chaos
        May 15 '15 at 12:28













      1












      1








      1






      The last two commands will never be executed in the shell of the oracle user. Try that:



      expect -c "
      log_user 0
      spawn su - oracle -c "ulimit -Hn"
      expect "Passwort:"
      send "$pass3n"
      expect eof
      puts "$expect_out(buffer)"
      "


      Also assuming that you only want the open files limit, with the -n flag (no need to grep, awk, ...).






      share|improve this answer














      The last two commands will never be executed in the shell of the oracle user. Try that:



      expect -c "
      log_user 0
      spawn su - oracle -c "ulimit -Hn"
      expect "Passwort:"
      send "$pass3n"
      expect eof
      puts "$expect_out(buffer)"
      "


      Also assuming that you only want the open files limit, with the -n flag (no need to grep, awk, ...).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 15 '15 at 12:18

























      answered May 15 '15 at 12:09









      chaos

      35.1k773116




      35.1k773116











      • it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
        – klerk
        May 15 '15 at 12:23










      • @klerk You can call the command within expect itself. send "your_command"...
        – chaos
        May 15 '15 at 12:28
















      • it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
        – klerk
        May 15 '15 at 12:23










      • @klerk You can call the command within expect itself. send "your_command"...
        – chaos
        May 15 '15 at 12:28















      it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
      – klerk
      May 15 '15 at 12:23




      it's not important what is after expect. you are saying that it is not possible to execute anything in oracle user shell ? any trick ?
      – klerk
      May 15 '15 at 12:23












      @klerk You can call the command within expect itself. send "your_command"...
      – chaos
      May 15 '15 at 12:28




      @klerk You can call the command within expect itself. send "your_command"...
      – chaos
      May 15 '15 at 12:28













      1














      This is largely untested. The aim is to send the commands you want to run as user "oracle" to the function as a parameter, and then get those to execute within the expect session.



      This uses single-quoted here-docs to contain both the commands you want to run as oracle and the expect body, so take care to match the quoting I'm demonstrating here.



      #!/bin/bash
      set -ex

      as_oracle()
      local pass="the_password"
      local cmds=$(cat -) # read the oracle commands from stdin

      # quoting in the next line is very important
      expect -f - "$pass" "$cmds" <<'END_EXPECT'
      lassign $argv pass3 commands

      spawn su - oracle
      expect "Password:"
      send -- "$pass3r"

      # this is the regular expression that matches the end
      # of oracle's prompt: please change it if it's not correct
      set prompt $ $

      expect -re $prompt
      send -- "eval "$commands"r"
      expect -re $prompt
      send -- "exitr"
      expect eof
      END_EXPECT



      as_oracle <<'END_ORACLE'
      var_ulimit=$(ulimit -Ha | awk '/open files/ print $4')
      echo $var_ulimit
      END_ORACLE


      I'm not certain that eval is the way to execute those commands. This may be better:



      send -- ". <(echo "$commands")r"





      share|improve this answer

























        1














        This is largely untested. The aim is to send the commands you want to run as user "oracle" to the function as a parameter, and then get those to execute within the expect session.



        This uses single-quoted here-docs to contain both the commands you want to run as oracle and the expect body, so take care to match the quoting I'm demonstrating here.



        #!/bin/bash
        set -ex

        as_oracle()
        local pass="the_password"
        local cmds=$(cat -) # read the oracle commands from stdin

        # quoting in the next line is very important
        expect -f - "$pass" "$cmds" <<'END_EXPECT'
        lassign $argv pass3 commands

        spawn su - oracle
        expect "Password:"
        send -- "$pass3r"

        # this is the regular expression that matches the end
        # of oracle's prompt: please change it if it's not correct
        set prompt $ $

        expect -re $prompt
        send -- "eval "$commands"r"
        expect -re $prompt
        send -- "exitr"
        expect eof
        END_EXPECT



        as_oracle <<'END_ORACLE'
        var_ulimit=$(ulimit -Ha | awk '/open files/ print $4')
        echo $var_ulimit
        END_ORACLE


        I'm not certain that eval is the way to execute those commands. This may be better:



        send -- ". <(echo "$commands")r"





        share|improve this answer























          1












          1








          1






          This is largely untested. The aim is to send the commands you want to run as user "oracle" to the function as a parameter, and then get those to execute within the expect session.



          This uses single-quoted here-docs to contain both the commands you want to run as oracle and the expect body, so take care to match the quoting I'm demonstrating here.



          #!/bin/bash
          set -ex

          as_oracle()
          local pass="the_password"
          local cmds=$(cat -) # read the oracle commands from stdin

          # quoting in the next line is very important
          expect -f - "$pass" "$cmds" <<'END_EXPECT'
          lassign $argv pass3 commands

          spawn su - oracle
          expect "Password:"
          send -- "$pass3r"

          # this is the regular expression that matches the end
          # of oracle's prompt: please change it if it's not correct
          set prompt $ $

          expect -re $prompt
          send -- "eval "$commands"r"
          expect -re $prompt
          send -- "exitr"
          expect eof
          END_EXPECT



          as_oracle <<'END_ORACLE'
          var_ulimit=$(ulimit -Ha | awk '/open files/ print $4')
          echo $var_ulimit
          END_ORACLE


          I'm not certain that eval is the way to execute those commands. This may be better:



          send -- ". <(echo "$commands")r"





          share|improve this answer












          This is largely untested. The aim is to send the commands you want to run as user "oracle" to the function as a parameter, and then get those to execute within the expect session.



          This uses single-quoted here-docs to contain both the commands you want to run as oracle and the expect body, so take care to match the quoting I'm demonstrating here.



          #!/bin/bash
          set -ex

          as_oracle()
          local pass="the_password"
          local cmds=$(cat -) # read the oracle commands from stdin

          # quoting in the next line is very important
          expect -f - "$pass" "$cmds" <<'END_EXPECT'
          lassign $argv pass3 commands

          spawn su - oracle
          expect "Password:"
          send -- "$pass3r"

          # this is the regular expression that matches the end
          # of oracle's prompt: please change it if it's not correct
          set prompt $ $

          expect -re $prompt
          send -- "eval "$commands"r"
          expect -re $prompt
          send -- "exitr"
          expect eof
          END_EXPECT



          as_oracle <<'END_ORACLE'
          var_ulimit=$(ulimit -Ha | awk '/open files/ print $4')
          echo $var_ulimit
          END_ORACLE


          I'm not certain that eval is the way to execute those commands. This may be better:



          send -- ". <(echo "$commands")r"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 15 '15 at 14:34









          glenn jackman

          50.3k570107




          50.3k570107





















              0














              Create a new script file for your expect script like "expect.sh".
              And call it in your bash script:



              ./expect.sh


              Now your expect script will work in your bash script.






              share|improve this answer

























                0














                Create a new script file for your expect script like "expect.sh".
                And call it in your bash script:



                ./expect.sh


                Now your expect script will work in your bash script.






                share|improve this answer























                  0












                  0








                  0






                  Create a new script file for your expect script like "expect.sh".
                  And call it in your bash script:



                  ./expect.sh


                  Now your expect script will work in your bash script.






                  share|improve this answer












                  Create a new script file for your expect script like "expect.sh".
                  And call it in your bash script:



                  ./expect.sh


                  Now your expect script will work in your bash script.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 21 '18 at 9:03









                  Gamze Helin Aslan

                  1




                  1



























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f203594%2fhow-to-continue-bash-script-execution-when-expect-part-is-finished%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