Running a .sh to execute multiple commands












8















I was curious how I would go about running multiple commands via shell script.



For example, just to try it out, I'd like to be able to connect a .desktop to a .sh that will run all the update codes so I don't have to type it out. I know how to execute a single command via a .sh, not multiple.



Any way?










share|improve this question



























    8















    I was curious how I would go about running multiple commands via shell script.



    For example, just to try it out, I'd like to be able to connect a .desktop to a .sh that will run all the update codes so I don't have to type it out. I know how to execute a single command via a .sh, not multiple.



    Any way?










    share|improve this question

























      8












      8








      8


      3






      I was curious how I would go about running multiple commands via shell script.



      For example, just to try it out, I'd like to be able to connect a .desktop to a .sh that will run all the update codes so I don't have to type it out. I know how to execute a single command via a .sh, not multiple.



      Any way?










      share|improve this question














      I was curious how I would go about running multiple commands via shell script.



      For example, just to try it out, I'd like to be able to connect a .desktop to a .sh that will run all the update codes so I don't have to type it out. I know how to execute a single command via a .sh, not multiple.



      Any way?







      command-line bash .desktop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 19 '14 at 23:50









      BrendonBrendon

      50118




      50118






















          2 Answers
          2






          active

          oldest

          votes


















          9














          Simple really - you need to separate the commands. For instance this:



          #!/bin/bash
          sudo apt-get update
          sudo apt-get upgrade


          will update the package lists, and then the packages. Another possibility is this (probably better, as the && will only allow the upgrade command to run if the update exits successfully):



          #!/bin/bash
          sudo apt-get update && sudo apt-get upgrade


          This should run the commands for updating the list of packages and then the package themselves. This would need to be saved into a text file (e.g. updatescript in the home directory). You can then make it executable by running:



           chmod +x ./updatescript


          or by right clicking on the file and making it executable in its properties:





          and run it with this in terminal:



           ./updatescript


          Note(s):



          There is an option (-y) available that can be used with the upgrade command so it does not prompt before upgrading the packages. It is a good idea to check what apt-get is doing before it upgrades the packages, as it may/will do something stupid or something you don't want it do. More info on options like this can be found by running man apt-get.



          This also means it is more ideal to use the terminal to run the script, as you can check what it is updating, and then run it. If you want a GUI way to run the updates, use the software-center as that it is what it is for.






          share|improve this answer































            4














            Just to add a few points to the excellent answer by @Wilf,



            you can run commands in paralell if you want for any reason, for example, an autostart script for OpenBox might look like this:



            #!/bin/bash

            nitrogen --restore &
            tint2 &
            tilda


            This will set a background image using nitrogen, start the panel tint2 and start the Quake3-style dropdown console tilda, all at the same time. This is useful if you don't need a program to finish before you launch the next one. Not the case for apt-get update and apt-get upgrade though!



            Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it's different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.





            Also, you could just stack commands to have them executed serially, like so:



            #!/bin/bash

            apt-get update
            apt-get upgrade


            This will run apt-get upgrade after apt-get update is finished, but regardless of the result. So even if, for example, the update quits with a network error, the upgrade will still be run.



            You can also do this in one line, using the ; sign: apt-get update ; apt-get upgrade. Again, this solution is not optimal for these particular commands, but it might come in handy in other situations.





            One quick final point, you can also break lines containing && as such:



            command_1 &&
            command_2


            This will be the same as command_1 && command_2.






            share|improve this answer

























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              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%2faskubuntu.com%2fquestions%2f500071%2frunning-a-sh-to-execute-multiple-commands%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









              9














              Simple really - you need to separate the commands. For instance this:



              #!/bin/bash
              sudo apt-get update
              sudo apt-get upgrade


              will update the package lists, and then the packages. Another possibility is this (probably better, as the && will only allow the upgrade command to run if the update exits successfully):



              #!/bin/bash
              sudo apt-get update && sudo apt-get upgrade


              This should run the commands for updating the list of packages and then the package themselves. This would need to be saved into a text file (e.g. updatescript in the home directory). You can then make it executable by running:



               chmod +x ./updatescript


              or by right clicking on the file and making it executable in its properties:





              and run it with this in terminal:



               ./updatescript


              Note(s):



              There is an option (-y) available that can be used with the upgrade command so it does not prompt before upgrading the packages. It is a good idea to check what apt-get is doing before it upgrades the packages, as it may/will do something stupid or something you don't want it do. More info on options like this can be found by running man apt-get.



              This also means it is more ideal to use the terminal to run the script, as you can check what it is updating, and then run it. If you want a GUI way to run the updates, use the software-center as that it is what it is for.






              share|improve this answer




























                9














                Simple really - you need to separate the commands. For instance this:



                #!/bin/bash
                sudo apt-get update
                sudo apt-get upgrade


                will update the package lists, and then the packages. Another possibility is this (probably better, as the && will only allow the upgrade command to run if the update exits successfully):



                #!/bin/bash
                sudo apt-get update && sudo apt-get upgrade


                This should run the commands for updating the list of packages and then the package themselves. This would need to be saved into a text file (e.g. updatescript in the home directory). You can then make it executable by running:



                 chmod +x ./updatescript


                or by right clicking on the file and making it executable in its properties:





                and run it with this in terminal:



                 ./updatescript


                Note(s):



                There is an option (-y) available that can be used with the upgrade command so it does not prompt before upgrading the packages. It is a good idea to check what apt-get is doing before it upgrades the packages, as it may/will do something stupid or something you don't want it do. More info on options like this can be found by running man apt-get.



                This also means it is more ideal to use the terminal to run the script, as you can check what it is updating, and then run it. If you want a GUI way to run the updates, use the software-center as that it is what it is for.






                share|improve this answer


























                  9












                  9








                  9







                  Simple really - you need to separate the commands. For instance this:



                  #!/bin/bash
                  sudo apt-get update
                  sudo apt-get upgrade


                  will update the package lists, and then the packages. Another possibility is this (probably better, as the && will only allow the upgrade command to run if the update exits successfully):



                  #!/bin/bash
                  sudo apt-get update && sudo apt-get upgrade


                  This should run the commands for updating the list of packages and then the package themselves. This would need to be saved into a text file (e.g. updatescript in the home directory). You can then make it executable by running:



                   chmod +x ./updatescript


                  or by right clicking on the file and making it executable in its properties:





                  and run it with this in terminal:



                   ./updatescript


                  Note(s):



                  There is an option (-y) available that can be used with the upgrade command so it does not prompt before upgrading the packages. It is a good idea to check what apt-get is doing before it upgrades the packages, as it may/will do something stupid or something you don't want it do. More info on options like this can be found by running man apt-get.



                  This also means it is more ideal to use the terminal to run the script, as you can check what it is updating, and then run it. If you want a GUI way to run the updates, use the software-center as that it is what it is for.






                  share|improve this answer













                  Simple really - you need to separate the commands. For instance this:



                  #!/bin/bash
                  sudo apt-get update
                  sudo apt-get upgrade


                  will update the package lists, and then the packages. Another possibility is this (probably better, as the && will only allow the upgrade command to run if the update exits successfully):



                  #!/bin/bash
                  sudo apt-get update && sudo apt-get upgrade


                  This should run the commands for updating the list of packages and then the package themselves. This would need to be saved into a text file (e.g. updatescript in the home directory). You can then make it executable by running:



                   chmod +x ./updatescript


                  or by right clicking on the file and making it executable in its properties:





                  and run it with this in terminal:



                   ./updatescript


                  Note(s):



                  There is an option (-y) available that can be used with the upgrade command so it does not prompt before upgrading the packages. It is a good idea to check what apt-get is doing before it upgrades the packages, as it may/will do something stupid or something you don't want it do. More info on options like this can be found by running man apt-get.



                  This also means it is more ideal to use the terminal to run the script, as you can check what it is updating, and then run it. If you want a GUI way to run the updates, use the software-center as that it is what it is for.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 20 '14 at 1:14









                  WilfWilf

                  21.3k1066129




                  21.3k1066129

























                      4














                      Just to add a few points to the excellent answer by @Wilf,



                      you can run commands in paralell if you want for any reason, for example, an autostart script for OpenBox might look like this:



                      #!/bin/bash

                      nitrogen --restore &
                      tint2 &
                      tilda


                      This will set a background image using nitrogen, start the panel tint2 and start the Quake3-style dropdown console tilda, all at the same time. This is useful if you don't need a program to finish before you launch the next one. Not the case for apt-get update and apt-get upgrade though!



                      Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it's different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.





                      Also, you could just stack commands to have them executed serially, like so:



                      #!/bin/bash

                      apt-get update
                      apt-get upgrade


                      This will run apt-get upgrade after apt-get update is finished, but regardless of the result. So even if, for example, the update quits with a network error, the upgrade will still be run.



                      You can also do this in one line, using the ; sign: apt-get update ; apt-get upgrade. Again, this solution is not optimal for these particular commands, but it might come in handy in other situations.





                      One quick final point, you can also break lines containing && as such:



                      command_1 &&
                      command_2


                      This will be the same as command_1 && command_2.






                      share|improve this answer






























                        4














                        Just to add a few points to the excellent answer by @Wilf,



                        you can run commands in paralell if you want for any reason, for example, an autostart script for OpenBox might look like this:



                        #!/bin/bash

                        nitrogen --restore &
                        tint2 &
                        tilda


                        This will set a background image using nitrogen, start the panel tint2 and start the Quake3-style dropdown console tilda, all at the same time. This is useful if you don't need a program to finish before you launch the next one. Not the case for apt-get update and apt-get upgrade though!



                        Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it's different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.





                        Also, you could just stack commands to have them executed serially, like so:



                        #!/bin/bash

                        apt-get update
                        apt-get upgrade


                        This will run apt-get upgrade after apt-get update is finished, but regardless of the result. So even if, for example, the update quits with a network error, the upgrade will still be run.



                        You can also do this in one line, using the ; sign: apt-get update ; apt-get upgrade. Again, this solution is not optimal for these particular commands, but it might come in handy in other situations.





                        One quick final point, you can also break lines containing && as such:



                        command_1 &&
                        command_2


                        This will be the same as command_1 && command_2.






                        share|improve this answer




























                          4












                          4








                          4







                          Just to add a few points to the excellent answer by @Wilf,



                          you can run commands in paralell if you want for any reason, for example, an autostart script for OpenBox might look like this:



                          #!/bin/bash

                          nitrogen --restore &
                          tint2 &
                          tilda


                          This will set a background image using nitrogen, start the panel tint2 and start the Quake3-style dropdown console tilda, all at the same time. This is useful if you don't need a program to finish before you launch the next one. Not the case for apt-get update and apt-get upgrade though!



                          Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it's different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.





                          Also, you could just stack commands to have them executed serially, like so:



                          #!/bin/bash

                          apt-get update
                          apt-get upgrade


                          This will run apt-get upgrade after apt-get update is finished, but regardless of the result. So even if, for example, the update quits with a network error, the upgrade will still be run.



                          You can also do this in one line, using the ; sign: apt-get update ; apt-get upgrade. Again, this solution is not optimal for these particular commands, but it might come in handy in other situations.





                          One quick final point, you can also break lines containing && as such:



                          command_1 &&
                          command_2


                          This will be the same as command_1 && command_2.






                          share|improve this answer















                          Just to add a few points to the excellent answer by @Wilf,



                          you can run commands in paralell if you want for any reason, for example, an autostart script for OpenBox might look like this:



                          #!/bin/bash

                          nitrogen --restore &
                          tint2 &
                          tilda


                          This will set a background image using nitrogen, start the panel tint2 and start the Quake3-style dropdown console tilda, all at the same time. This is useful if you don't need a program to finish before you launch the next one. Not the case for apt-get update and apt-get upgrade though!



                          Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it's different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.





                          Also, you could just stack commands to have them executed serially, like so:



                          #!/bin/bash

                          apt-get update
                          apt-get upgrade


                          This will run apt-get upgrade after apt-get update is finished, but regardless of the result. So even if, for example, the update quits with a network error, the upgrade will still be run.



                          You can also do this in one line, using the ; sign: apt-get update ; apt-get upgrade. Again, this solution is not optimal for these particular commands, but it might come in handy in other situations.





                          One quick final point, you can also break lines containing && as such:



                          command_1 &&
                          command_2


                          This will be the same as command_1 && command_2.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 20 '14 at 2:57

























                          answered Jul 20 '14 at 2:15









                          kraxorkraxor

                          4,27431832




                          4,27431832






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • 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%2faskubuntu.com%2fquestions%2f500071%2frunning-a-sh-to-execute-multiple-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







                              Popular posts from this blog

                              Quarter-circle Tiles

                              build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                              Mont Emei