run a shell script which reference another script on ssh

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












1














I have two bash scripts main.sh and a util.sh, and I reference utils.sh in the main.sh by using . util.sh. Now I want to run main.sh via ssh. Obviously, this won't work:




ssh user@host "bash /dev/stdin < main.sh"




Because remote shell cannot find util.sh. So how should I do this, without copying util.sh to remote machine ?










share|improve this question





















  • To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
    – nohillside
    Dec 24 '18 at 9:15










  • Maybe utils.sh is a common module and reused by many scripts
    – Lewis Chan
    Dec 24 '18 at 9:31






  • 1




    You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
    – nohillside
    Dec 24 '18 at 9:49















1














I have two bash scripts main.sh and a util.sh, and I reference utils.sh in the main.sh by using . util.sh. Now I want to run main.sh via ssh. Obviously, this won't work:




ssh user@host "bash /dev/stdin < main.sh"




Because remote shell cannot find util.sh. So how should I do this, without copying util.sh to remote machine ?










share|improve this question





















  • To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
    – nohillside
    Dec 24 '18 at 9:15










  • Maybe utils.sh is a common module and reused by many scripts
    – Lewis Chan
    Dec 24 '18 at 9:31






  • 1




    You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
    – nohillside
    Dec 24 '18 at 9:49













1












1








1







I have two bash scripts main.sh and a util.sh, and I reference utils.sh in the main.sh by using . util.sh. Now I want to run main.sh via ssh. Obviously, this won't work:




ssh user@host "bash /dev/stdin < main.sh"




Because remote shell cannot find util.sh. So how should I do this, without copying util.sh to remote machine ?










share|improve this question













I have two bash scripts main.sh and a util.sh, and I reference utils.sh in the main.sh by using . util.sh. Now I want to run main.sh via ssh. Obviously, this won't work:




ssh user@host "bash /dev/stdin < main.sh"




Because remote shell cannot find util.sh. So how should I do this, without copying util.sh to remote machine ?







bash shell ssh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 24 '18 at 9:13









Lewis Chan

61




61











  • To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
    – nohillside
    Dec 24 '18 at 9:15










  • Maybe utils.sh is a common module and reused by many scripts
    – Lewis Chan
    Dec 24 '18 at 9:31






  • 1




    You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
    – nohillside
    Dec 24 '18 at 9:49
















  • To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
    – nohillside
    Dec 24 '18 at 9:15










  • Maybe utils.sh is a common module and reused by many scripts
    – Lewis Chan
    Dec 24 '18 at 9:31






  • 1




    You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
    – nohillside
    Dec 24 '18 at 9:49















To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
– nohillside
Dec 24 '18 at 9:15




To run a remote script ssh user@host main.sh would be enough actually. Why can't you just copy util.sh over as well?
– nohillside
Dec 24 '18 at 9:15












Maybe utils.sh is a common module and reused by many scripts
– Lewis Chan
Dec 24 '18 at 9:31




Maybe utils.sh is a common module and reused by many scripts
– Lewis Chan
Dec 24 '18 at 9:31




1




1




You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
– nohillside
Dec 24 '18 at 9:49




You still can deploy it to host together with main.sh. Or put it onto a shared/network drive somewhere. Or have main.sh check whether it is available locally and download it from somewhere if not.
– nohillside
Dec 24 '18 at 9:49










2 Answers
2






active

oldest

votes


















0














Even though I think deploying util.sh to host would be the better/more stable solution, you could do something like putting this on top of main.sh



#!/bin/bash

U=$(mktemp)
cat > "$U"
. "$U"
rm -f "$U"
# rest of script


and then run



ssh user@host main.sh < util.sh


To be more fail-safe you may require some extra coding in main.sh to handle issues with the temp file being unwritable/undeletable, no input passed on stdinetc.






share|improve this answer




























    0














    This is ugly, but should work:



    ssh user@host bash < <(sed 's/. util.sh/ r util.sh; d' main.sh)


    That will embed the contents of the util script into the main script, and then pipe the script to the bash process running on the remote host.
    Note that I've removed the double quotes: the process substitution needs to run on your local machine where both files reside.






    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%2f490735%2frun-a-shell-script-which-reference-another-script-on-ssh%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Even though I think deploying util.sh to host would be the better/more stable solution, you could do something like putting this on top of main.sh



      #!/bin/bash

      U=$(mktemp)
      cat > "$U"
      . "$U"
      rm -f "$U"
      # rest of script


      and then run



      ssh user@host main.sh < util.sh


      To be more fail-safe you may require some extra coding in main.sh to handle issues with the temp file being unwritable/undeletable, no input passed on stdinetc.






      share|improve this answer

























        0














        Even though I think deploying util.sh to host would be the better/more stable solution, you could do something like putting this on top of main.sh



        #!/bin/bash

        U=$(mktemp)
        cat > "$U"
        . "$U"
        rm -f "$U"
        # rest of script


        and then run



        ssh user@host main.sh < util.sh


        To be more fail-safe you may require some extra coding in main.sh to handle issues with the temp file being unwritable/undeletable, no input passed on stdinetc.






        share|improve this answer























          0












          0








          0






          Even though I think deploying util.sh to host would be the better/more stable solution, you could do something like putting this on top of main.sh



          #!/bin/bash

          U=$(mktemp)
          cat > "$U"
          . "$U"
          rm -f "$U"
          # rest of script


          and then run



          ssh user@host main.sh < util.sh


          To be more fail-safe you may require some extra coding in main.sh to handle issues with the temp file being unwritable/undeletable, no input passed on stdinetc.






          share|improve this answer












          Even though I think deploying util.sh to host would be the better/more stable solution, you could do something like putting this on top of main.sh



          #!/bin/bash

          U=$(mktemp)
          cat > "$U"
          . "$U"
          rm -f "$U"
          # rest of script


          and then run



          ssh user@host main.sh < util.sh


          To be more fail-safe you may require some extra coding in main.sh to handle issues with the temp file being unwritable/undeletable, no input passed on stdinetc.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 24 '18 at 10:05









          nohillside

          2,382919




          2,382919























              0














              This is ugly, but should work:



              ssh user@host bash < <(sed 's/. util.sh/ r util.sh; d' main.sh)


              That will embed the contents of the util script into the main script, and then pipe the script to the bash process running on the remote host.
              Note that I've removed the double quotes: the process substitution needs to run on your local machine where both files reside.






              share|improve this answer

























                0














                This is ugly, but should work:



                ssh user@host bash < <(sed 's/. util.sh/ r util.sh; d' main.sh)


                That will embed the contents of the util script into the main script, and then pipe the script to the bash process running on the remote host.
                Note that I've removed the double quotes: the process substitution needs to run on your local machine where both files reside.






                share|improve this answer























                  0












                  0








                  0






                  This is ugly, but should work:



                  ssh user@host bash < <(sed 's/. util.sh/ r util.sh; d' main.sh)


                  That will embed the contents of the util script into the main script, and then pipe the script to the bash process running on the remote host.
                  Note that I've removed the double quotes: the process substitution needs to run on your local machine where both files reside.






                  share|improve this answer












                  This is ugly, but should work:



                  ssh user@host bash < <(sed 's/. util.sh/ r util.sh; d' main.sh)


                  That will embed the contents of the util script into the main script, and then pipe the script to the bash process running on the remote host.
                  Note that I've removed the double quotes: the process substitution needs to run on your local machine where both files reside.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 24 '18 at 13:20









                  glenn jackman

                  50.4k570107




                  50.4k570107



























                      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%2f490735%2frun-a-shell-script-which-reference-another-script-on-ssh%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