sh script for executing mysql script with entering pass

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











up vote
1
down vote

favorite












I'd like to execute a mysql script where the user is asked to enter the password dynamically from the command line, if possible with a message Enter your $root password.
However it doesn't work as expected. So far I have



mysql -u root -p CREATE DATABASE IF NOT EXISTS Test


moreover when the db is created how to logout from mysql and go back to bash commands?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I'd like to execute a mysql script where the user is asked to enter the password dynamically from the command line, if possible with a message Enter your $root password.
    However it doesn't work as expected. So far I have



    mysql -u root -p CREATE DATABASE IF NOT EXISTS Test


    moreover when the db is created how to logout from mysql and go back to bash commands?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'd like to execute a mysql script where the user is asked to enter the password dynamically from the command line, if possible with a message Enter your $root password.
      However it doesn't work as expected. So far I have



      mysql -u root -p CREATE DATABASE IF NOT EXISTS Test


      moreover when the db is created how to logout from mysql and go back to bash commands?










      share|improve this question















      I'd like to execute a mysql script where the user is asked to enter the password dynamically from the command line, if possible with a message Enter your $root password.
      However it doesn't work as expected. So far I have



      mysql -u root -p CREATE DATABASE IF NOT EXISTS Test


      moreover when the db is created how to logout from mysql and go back to bash commands?







      command-line bash scripts database sh






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 10 mins ago









      dessert

      19.9k55795




      19.9k55795










      asked 3 hours ago









      Jocky Doe

      1235




      1235




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          According to the docs



          mysql -u root -p --execute="SELECT User, Host FROM mysql.user"


          prompts for a password, executes the given command, and returns to the shell (no logout required).



          If you instead want your own prompt you need to script it:



          #!/usr/bin/env bash

          echo "Enter pwd"
          read pwd
          mysql -u root -p$pwd --execute="SELECT User, Host FROM mysql.user"


          Please note that there must be no space between -p and $pwd because
          mysql then considers $pwd to be the database's name.



          I would not recommend that because the password is then shown in
          cleartext in the terminal whereas mysql -p shows asterisks instead.






          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',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            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%2f1077725%2fsh-script-for-executing-mysql-script-with-entering-pass%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            According to the docs



            mysql -u root -p --execute="SELECT User, Host FROM mysql.user"


            prompts for a password, executes the given command, and returns to the shell (no logout required).



            If you instead want your own prompt you need to script it:



            #!/usr/bin/env bash

            echo "Enter pwd"
            read pwd
            mysql -u root -p$pwd --execute="SELECT User, Host FROM mysql.user"


            Please note that there must be no space between -p and $pwd because
            mysql then considers $pwd to be the database's name.



            I would not recommend that because the password is then shown in
            cleartext in the terminal whereas mysql -p shows asterisks instead.






            share|improve this answer


























              up vote
              3
              down vote



              accepted










              According to the docs



              mysql -u root -p --execute="SELECT User, Host FROM mysql.user"


              prompts for a password, executes the given command, and returns to the shell (no logout required).



              If you instead want your own prompt you need to script it:



              #!/usr/bin/env bash

              echo "Enter pwd"
              read pwd
              mysql -u root -p$pwd --execute="SELECT User, Host FROM mysql.user"


              Please note that there must be no space between -p and $pwd because
              mysql then considers $pwd to be the database's name.



              I would not recommend that because the password is then shown in
              cleartext in the terminal whereas mysql -p shows asterisks instead.






              share|improve this answer
























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                According to the docs



                mysql -u root -p --execute="SELECT User, Host FROM mysql.user"


                prompts for a password, executes the given command, and returns to the shell (no logout required).



                If you instead want your own prompt you need to script it:



                #!/usr/bin/env bash

                echo "Enter pwd"
                read pwd
                mysql -u root -p$pwd --execute="SELECT User, Host FROM mysql.user"


                Please note that there must be no space between -p and $pwd because
                mysql then considers $pwd to be the database's name.



                I would not recommend that because the password is then shown in
                cleartext in the terminal whereas mysql -p shows asterisks instead.






                share|improve this answer














                According to the docs



                mysql -u root -p --execute="SELECT User, Host FROM mysql.user"


                prompts for a password, executes the given command, and returns to the shell (no logout required).



                If you instead want your own prompt you need to script it:



                #!/usr/bin/env bash

                echo "Enter pwd"
                read pwd
                mysql -u root -p$pwd --execute="SELECT User, Host FROM mysql.user"


                Please note that there must be no space between -p and $pwd because
                mysql then considers $pwd to be the database's name.



                I would not recommend that because the password is then shown in
                cleartext in the terminal whereas mysql -p shows asterisks instead.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 9 mins ago









                dessert

                19.9k55795




                19.9k55795










                answered 2 hours ago









                PerlDuck

                4,00311030




                4,00311030



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1077725%2fsh-script-for-executing-mysql-script-with-entering-pass%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    What does second last employer means? [closed]

                    One-line joke