bash script user prompt for preferred directory












-1














I am attempting my first bash script. I would like to prompt the user to find out where they would like a cloned repo to be saved.



Currently I am assigning it like this.



warpToLocation="${HOME}/apps/"


Is there some way to do it like this:



read -e -p "Enter the path to the file: " -i "${HOME}/apps/" FILEPATH


but save the result as warpToLocation?



EDIT:



#!/bin/bash

echo "where would you like to install your repos?"

read -p "Enter the path to the file: " temp
warpToLocation="${HOME}/$temp/"

warpInLocations=("git@github.com:cca/toolkit.git" "git@github.com:cca/sms.git" "git@github.com:cca/boogle.git" "git@github.com:cca/cairo.git")



echo "warping in toolkit, sms, boogle and cairo"
for repo in "${warpInLocations[@]}"
do
warpInDir=$repo
warpInDir=${warpInDir#*/}
warpInDir=${warpInDir%.*}
if [ -d "$warpToLocation"]; then
echo "somethings in the way.. $warpInDir all ready exists"
else
git clone $repo $warpInDir
fi

done


What I had done to get that error was add the code you gave me.



The issue with that is that -e (which allows you to edit the input with arrows) and -i (a preview/optional answer) runs on bash version 4 and higher and I am running version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12).










share|improve this question





























    -1














    I am attempting my first bash script. I would like to prompt the user to find out where they would like a cloned repo to be saved.



    Currently I am assigning it like this.



    warpToLocation="${HOME}/apps/"


    Is there some way to do it like this:



    read -e -p "Enter the path to the file: " -i "${HOME}/apps/" FILEPATH


    but save the result as warpToLocation?



    EDIT:



    #!/bin/bash

    echo "where would you like to install your repos?"

    read -p "Enter the path to the file: " temp
    warpToLocation="${HOME}/$temp/"

    warpInLocations=("git@github.com:cca/toolkit.git" "git@github.com:cca/sms.git" "git@github.com:cca/boogle.git" "git@github.com:cca/cairo.git")



    echo "warping in toolkit, sms, boogle and cairo"
    for repo in "${warpInLocations[@]}"
    do
    warpInDir=$repo
    warpInDir=${warpInDir#*/}
    warpInDir=${warpInDir%.*}
    if [ -d "$warpToLocation"]; then
    echo "somethings in the way.. $warpInDir all ready exists"
    else
    git clone $repo $warpInDir
    fi

    done


    What I had done to get that error was add the code you gave me.



    The issue with that is that -e (which allows you to edit the input with arrows) and -i (a preview/optional answer) runs on bash version 4 and higher and I am running version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12).










    share|improve this question



























      -1












      -1








      -1







      I am attempting my first bash script. I would like to prompt the user to find out where they would like a cloned repo to be saved.



      Currently I am assigning it like this.



      warpToLocation="${HOME}/apps/"


      Is there some way to do it like this:



      read -e -p "Enter the path to the file: " -i "${HOME}/apps/" FILEPATH


      but save the result as warpToLocation?



      EDIT:



      #!/bin/bash

      echo "where would you like to install your repos?"

      read -p "Enter the path to the file: " temp
      warpToLocation="${HOME}/$temp/"

      warpInLocations=("git@github.com:cca/toolkit.git" "git@github.com:cca/sms.git" "git@github.com:cca/boogle.git" "git@github.com:cca/cairo.git")



      echo "warping in toolkit, sms, boogle and cairo"
      for repo in "${warpInLocations[@]}"
      do
      warpInDir=$repo
      warpInDir=${warpInDir#*/}
      warpInDir=${warpInDir%.*}
      if [ -d "$warpToLocation"]; then
      echo "somethings in the way.. $warpInDir all ready exists"
      else
      git clone $repo $warpInDir
      fi

      done


      What I had done to get that error was add the code you gave me.



      The issue with that is that -e (which allows you to edit the input with arrows) and -i (a preview/optional answer) runs on bash version 4 and higher and I am running version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12).










      share|improve this question















      I am attempting my first bash script. I would like to prompt the user to find out where they would like a cloned repo to be saved.



      Currently I am assigning it like this.



      warpToLocation="${HOME}/apps/"


      Is there some way to do it like this:



      read -e -p "Enter the path to the file: " -i "${HOME}/apps/" FILEPATH


      but save the result as warpToLocation?



      EDIT:



      #!/bin/bash

      echo "where would you like to install your repos?"

      read -p "Enter the path to the file: " temp
      warpToLocation="${HOME}/$temp/"

      warpInLocations=("git@github.com:cca/toolkit.git" "git@github.com:cca/sms.git" "git@github.com:cca/boogle.git" "git@github.com:cca/cairo.git")



      echo "warping in toolkit, sms, boogle and cairo"
      for repo in "${warpInLocations[@]}"
      do
      warpInDir=$repo
      warpInDir=${warpInDir#*/}
      warpInDir=${warpInDir%.*}
      if [ -d "$warpToLocation"]; then
      echo "somethings in the way.. $warpInDir all ready exists"
      else
      git clone $repo $warpInDir
      fi

      done


      What I had done to get that error was add the code you gave me.



      The issue with that is that -e (which allows you to edit the input with arrows) and -i (a preview/optional answer) runs on bash version 4 and higher and I am running version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12).







      bash scripting prompt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 at 12:09









      Kusalananda

      121k16229372




      121k16229372










      asked May 24 '13 at 14:40









      TheLegend

      1,5423914




      1,5423914






















          2 Answers
          2






          active

          oldest

          votes


















          0














          What's wrong with



          read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation


          ?






          share|improve this answer





















          • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
            – TheLegend
            May 24 '13 at 14:51












          • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
            – TheLegend
            May 24 '13 at 14:53










          • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
            – Uwe
            May 24 '13 at 15:05










          • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
            – TheLegend
            May 24 '13 at 15:26










          • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
            – Uwe
            May 24 '13 at 15:30



















          0














          It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.



          Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.



          #!/bin/sh

          destdir=$1

          if [ ! -d "$destdir" ]; then
          printf 'No such directory: %sn' "$destdir" >&2
          exit 1
          fi

          for repo in toolkit sms boggle cairo
          do
          if [ -e "$destdir/$repo" ]; then
          printf 'Name %s already exists for repository %s (skipping)n'
          "$destdir/$repo" "$repo" >&2
          continue
          fi

          printf 'Cloning %sn' "$repo"
          git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
          done


          This script would be used as



          ./script.sh "$HOME/projects/stuff"


          and could be run without user interaction from e.g. Ansible or Cron.






          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%2f76978%2fbash-script-user-prompt-for-preferred-directory%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














            What's wrong with



            read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation


            ?






            share|improve this answer





















            • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
              – TheLegend
              May 24 '13 at 14:51












            • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
              – TheLegend
              May 24 '13 at 14:53










            • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
              – Uwe
              May 24 '13 at 15:05










            • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
              – TheLegend
              May 24 '13 at 15:26










            • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
              – Uwe
              May 24 '13 at 15:30
















            0














            What's wrong with



            read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation


            ?






            share|improve this answer





















            • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
              – TheLegend
              May 24 '13 at 14:51












            • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
              – TheLegend
              May 24 '13 at 14:53










            • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
              – Uwe
              May 24 '13 at 15:05










            • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
              – TheLegend
              May 24 '13 at 15:26










            • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
              – Uwe
              May 24 '13 at 15:30














            0












            0








            0






            What's wrong with



            read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation


            ?






            share|improve this answer












            What's wrong with



            read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation


            ?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 24 '13 at 14:46









            Uwe

            3,0171217




            3,0171217












            • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
              – TheLegend
              May 24 '13 at 14:51












            • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
              – TheLegend
              May 24 '13 at 14:53










            • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
              – Uwe
              May 24 '13 at 15:05










            • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
              – TheLegend
              May 24 '13 at 15:26










            • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
              – Uwe
              May 24 '13 at 15:30


















            • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
              – TheLegend
              May 24 '13 at 14:51












            • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
              – TheLegend
              May 24 '13 at 14:53










            • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
              – Uwe
              May 24 '13 at 15:05










            • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
              – TheLegend
              May 24 '13 at 15:26










            • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
              – Uwe
              May 24 '13 at 15:30
















            so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
            – TheLegend
            May 24 '13 at 14:51






            so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os
            – TheLegend
            May 24 '13 at 14:51














            invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
            – TheLegend
            May 24 '13 at 14:53




            invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo
            – TheLegend
            May 24 '13 at 14:53












            I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
            – Uwe
            May 24 '13 at 15:05




            I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that.
            – Uwe
            May 24 '13 at 15:05












            that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
            – TheLegend
            May 24 '13 at 15:26




            that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation
            – TheLegend
            May 24 '13 at 15:26












            Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
            – Uwe
            May 24 '13 at 15:30




            Your read doesn't have a -i option? Are you really using bash? Which version? bash --version
            – Uwe
            May 24 '13 at 15:30













            0














            It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.



            Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.



            #!/bin/sh

            destdir=$1

            if [ ! -d "$destdir" ]; then
            printf 'No such directory: %sn' "$destdir" >&2
            exit 1
            fi

            for repo in toolkit sms boggle cairo
            do
            if [ -e "$destdir/$repo" ]; then
            printf 'Name %s already exists for repository %s (skipping)n'
            "$destdir/$repo" "$repo" >&2
            continue
            fi

            printf 'Cloning %sn' "$repo"
            git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
            done


            This script would be used as



            ./script.sh "$HOME/projects/stuff"


            and could be run without user interaction from e.g. Ansible or Cron.






            share|improve this answer




























              0














              It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.



              Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.



              #!/bin/sh

              destdir=$1

              if [ ! -d "$destdir" ]; then
              printf 'No such directory: %sn' "$destdir" >&2
              exit 1
              fi

              for repo in toolkit sms boggle cairo
              do
              if [ -e "$destdir/$repo" ]; then
              printf 'Name %s already exists for repository %s (skipping)n'
              "$destdir/$repo" "$repo" >&2
              continue
              fi

              printf 'Cloning %sn' "$repo"
              git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
              done


              This script would be used as



              ./script.sh "$HOME/projects/stuff"


              and could be run without user interaction from e.g. Ansible or Cron.






              share|improve this answer


























                0












                0








                0






                It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.



                Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.



                #!/bin/sh

                destdir=$1

                if [ ! -d "$destdir" ]; then
                printf 'No such directory: %sn' "$destdir" >&2
                exit 1
                fi

                for repo in toolkit sms boggle cairo
                do
                if [ -e "$destdir/$repo" ]; then
                printf 'Name %s already exists for repository %s (skipping)n'
                "$destdir/$repo" "$repo" >&2
                continue
                fi

                printf 'Cloning %sn' "$repo"
                git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
                done


                This script would be used as



                ./script.sh "$HOME/projects/stuff"


                and could be run without user interaction from e.g. Ansible or Cron.






                share|improve this answer














                It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.



                Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.



                #!/bin/sh

                destdir=$1

                if [ ! -d "$destdir" ]; then
                printf 'No such directory: %sn' "$destdir" >&2
                exit 1
                fi

                for repo in toolkit sms boggle cairo
                do
                if [ -e "$destdir/$repo" ]; then
                printf 'Name %s already exists for repository %s (skipping)n'
                "$destdir/$repo" "$repo" >&2
                continue
                fi

                printf 'Cloning %sn' "$repo"
                git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
                done


                This script would be used as



                ./script.sh "$HOME/projects/stuff"


                and could be run without user interaction from e.g. Ansible or Cron.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 16 at 12:04

























                answered Dec 16 at 11:50









                Kusalananda

                121k16229372




                121k16229372






























                    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%2f76978%2fbash-script-user-prompt-for-preferred-directory%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