Creating a shell script inside a text editor












1














I want to know how to create a shell script inside a text editor.



So this is what I have inside the text editor.



#!/bin/bash
mkdir -p temp
cd temp

if [ $1 > $2 ] ;
then
echo $1
else
echo $2
fi

./max.sh 4 6
./max.sh -2 -5
./max.sh 7 -3


So basically inside the text editor I want to create a shell script called max.sh so that below it I can pass arguments through it but in the same text editor.



To make it more clear:



I want the if-statement to be inside a script called max.sh, so below it I can call the max.sh with arguments and it will work.










share|improve this question





























    1














    I want to know how to create a shell script inside a text editor.



    So this is what I have inside the text editor.



    #!/bin/bash
    mkdir -p temp
    cd temp

    if [ $1 > $2 ] ;
    then
    echo $1
    else
    echo $2
    fi

    ./max.sh 4 6
    ./max.sh -2 -5
    ./max.sh 7 -3


    So basically inside the text editor I want to create a shell script called max.sh so that below it I can pass arguments through it but in the same text editor.



    To make it more clear:



    I want the if-statement to be inside a script called max.sh, so below it I can call the max.sh with arguments and it will work.










    share|improve this question



























      1












      1








      1







      I want to know how to create a shell script inside a text editor.



      So this is what I have inside the text editor.



      #!/bin/bash
      mkdir -p temp
      cd temp

      if [ $1 > $2 ] ;
      then
      echo $1
      else
      echo $2
      fi

      ./max.sh 4 6
      ./max.sh -2 -5
      ./max.sh 7 -3


      So basically inside the text editor I want to create a shell script called max.sh so that below it I can pass arguments through it but in the same text editor.



      To make it more clear:



      I want the if-statement to be inside a script called max.sh, so below it I can call the max.sh with arguments and it will work.










      share|improve this question















      I want to know how to create a shell script inside a text editor.



      So this is what I have inside the text editor.



      #!/bin/bash
      mkdir -p temp
      cd temp

      if [ $1 > $2 ] ;
      then
      echo $1
      else
      echo $2
      fi

      ./max.sh 4 6
      ./max.sh -2 -5
      ./max.sh 7 -3


      So basically inside the text editor I want to create a shell script called max.sh so that below it I can pass arguments through it but in the same text editor.



      To make it more clear:



      I want the if-statement to be inside a script called max.sh, so below it I can call the max.sh with arguments and it will work.







      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 at 11:50









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Mar 3 '16 at 23:06









      shawn edward

      105613




      105613






















          3 Answers
          3






          active

          oldest

          votes


















          10














          What you want is called a function:



          #!/bin/bash

          max() {
          if [ "$1" -gt "$2" ] ;
          then
          printf %s\n "$1"
          else
          printf %s\n "$2"
          fi
          }

          max 4 6
          max -2 -5
          max 7 -3


          Further reading:




          • In Bash, when to alias, when to script, and when to write a function?

          • Why is printf better than echo?

          • Why does my shell script choke on whitespace or other special characters?






          share|improve this answer































            3














            You can do what you ask like this:



            #!/bin/bash
            mkdir -p temp
            cd temp

            cat <<_script_lines_ > max.sh
            #!/bin/bash
            if [ "$1" -gt "$2" ] ;
            then
            printf '%sn' "$1"
            else
            printf '%sn' "$2"
            fi
            _script_lines_

            chmod u+x max.sh ### make the script excutable.

            # Use the script:
            ./max.sh 4 6
            ./max.sh -2 -5
            ./max.sh 7 -3


            But the function already recommended by Wildcard seems more reasonable to use.






            share|improve this answer























            • this works as well, thanks for the suggestion, appreciate it
              – shawn edward
              Mar 3 '16 at 23:42






            • 1




              @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
              – Wildcard
              Mar 3 '16 at 23:52



















            0














            If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.



            I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.






            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%2f267507%2fcreating-a-shell-script-inside-a-text-editor%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









              10














              What you want is called a function:



              #!/bin/bash

              max() {
              if [ "$1" -gt "$2" ] ;
              then
              printf %s\n "$1"
              else
              printf %s\n "$2"
              fi
              }

              max 4 6
              max -2 -5
              max 7 -3


              Further reading:




              • In Bash, when to alias, when to script, and when to write a function?

              • Why is printf better than echo?

              • Why does my shell script choke on whitespace or other special characters?






              share|improve this answer




























                10














                What you want is called a function:



                #!/bin/bash

                max() {
                if [ "$1" -gt "$2" ] ;
                then
                printf %s\n "$1"
                else
                printf %s\n "$2"
                fi
                }

                max 4 6
                max -2 -5
                max 7 -3


                Further reading:




                • In Bash, when to alias, when to script, and when to write a function?

                • Why is printf better than echo?

                • Why does my shell script choke on whitespace or other special characters?






                share|improve this answer


























                  10












                  10








                  10






                  What you want is called a function:



                  #!/bin/bash

                  max() {
                  if [ "$1" -gt "$2" ] ;
                  then
                  printf %s\n "$1"
                  else
                  printf %s\n "$2"
                  fi
                  }

                  max 4 6
                  max -2 -5
                  max 7 -3


                  Further reading:




                  • In Bash, when to alias, when to script, and when to write a function?

                  • Why is printf better than echo?

                  • Why does my shell script choke on whitespace or other special characters?






                  share|improve this answer














                  What you want is called a function:



                  #!/bin/bash

                  max() {
                  if [ "$1" -gt "$2" ] ;
                  then
                  printf %s\n "$1"
                  else
                  printf %s\n "$2"
                  fi
                  }

                  max 4 6
                  max -2 -5
                  max 7 -3


                  Further reading:




                  • In Bash, when to alias, when to script, and when to write a function?

                  • Why is printf better than echo?

                  • Why does my shell script choke on whitespace or other special characters?







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 13 '17 at 12:37









                  Community

                  1




                  1










                  answered Mar 3 '16 at 23:14









                  Wildcard

                  22.6k961164




                  22.6k961164

























                      3














                      You can do what you ask like this:



                      #!/bin/bash
                      mkdir -p temp
                      cd temp

                      cat <<_script_lines_ > max.sh
                      #!/bin/bash
                      if [ "$1" -gt "$2" ] ;
                      then
                      printf '%sn' "$1"
                      else
                      printf '%sn' "$2"
                      fi
                      _script_lines_

                      chmod u+x max.sh ### make the script excutable.

                      # Use the script:
                      ./max.sh 4 6
                      ./max.sh -2 -5
                      ./max.sh 7 -3


                      But the function already recommended by Wildcard seems more reasonable to use.






                      share|improve this answer























                      • this works as well, thanks for the suggestion, appreciate it
                        – shawn edward
                        Mar 3 '16 at 23:42






                      • 1




                        @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                        – Wildcard
                        Mar 3 '16 at 23:52
















                      3














                      You can do what you ask like this:



                      #!/bin/bash
                      mkdir -p temp
                      cd temp

                      cat <<_script_lines_ > max.sh
                      #!/bin/bash
                      if [ "$1" -gt "$2" ] ;
                      then
                      printf '%sn' "$1"
                      else
                      printf '%sn' "$2"
                      fi
                      _script_lines_

                      chmod u+x max.sh ### make the script excutable.

                      # Use the script:
                      ./max.sh 4 6
                      ./max.sh -2 -5
                      ./max.sh 7 -3


                      But the function already recommended by Wildcard seems more reasonable to use.






                      share|improve this answer























                      • this works as well, thanks for the suggestion, appreciate it
                        – shawn edward
                        Mar 3 '16 at 23:42






                      • 1




                        @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                        – Wildcard
                        Mar 3 '16 at 23:52














                      3












                      3








                      3






                      You can do what you ask like this:



                      #!/bin/bash
                      mkdir -p temp
                      cd temp

                      cat <<_script_lines_ > max.sh
                      #!/bin/bash
                      if [ "$1" -gt "$2" ] ;
                      then
                      printf '%sn' "$1"
                      else
                      printf '%sn' "$2"
                      fi
                      _script_lines_

                      chmod u+x max.sh ### make the script excutable.

                      # Use the script:
                      ./max.sh 4 6
                      ./max.sh -2 -5
                      ./max.sh 7 -3


                      But the function already recommended by Wildcard seems more reasonable to use.






                      share|improve this answer














                      You can do what you ask like this:



                      #!/bin/bash
                      mkdir -p temp
                      cd temp

                      cat <<_script_lines_ > max.sh
                      #!/bin/bash
                      if [ "$1" -gt "$2" ] ;
                      then
                      printf '%sn' "$1"
                      else
                      printf '%sn' "$2"
                      fi
                      _script_lines_

                      chmod u+x max.sh ### make the script excutable.

                      # Use the script:
                      ./max.sh 4 6
                      ./max.sh -2 -5
                      ./max.sh 7 -3


                      But the function already recommended by Wildcard seems more reasonable to use.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 3 '16 at 23:42

























                      answered Mar 3 '16 at 23:27







                      user79743



















                      • this works as well, thanks for the suggestion, appreciate it
                        – shawn edward
                        Mar 3 '16 at 23:42






                      • 1




                        @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                        – Wildcard
                        Mar 3 '16 at 23:52


















                      • this works as well, thanks for the suggestion, appreciate it
                        – shawn edward
                        Mar 3 '16 at 23:42






                      • 1




                        @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                        – Wildcard
                        Mar 3 '16 at 23:52
















                      this works as well, thanks for the suggestion, appreciate it
                      – shawn edward
                      Mar 3 '16 at 23:42




                      this works as well, thanks for the suggestion, appreciate it
                      – shawn edward
                      Mar 3 '16 at 23:42




                      1




                      1




                      @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                      – Wildcard
                      Mar 3 '16 at 23:52




                      @shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole.
                      – Wildcard
                      Mar 3 '16 at 23:52











                      0














                      If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.



                      I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.






                      share|improve this answer


























                        0














                        If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.



                        I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.






                        share|improve this answer
























                          0












                          0








                          0






                          If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.



                          I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.






                          share|improve this answer












                          If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.



                          I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 4 '16 at 9:34









                          Arif Burhan

                          1011




                          1011






























                              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%2f267507%2fcreating-a-shell-script-inside-a-text-editor%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

                              Morgemoulin

                              Scott Moir

                              Souastre