How to get a list of subdirectories from a file and then create those subdirectories in a directory?










0















When a user inputs a name, there should be a new directory that gets created under that name.

In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.

In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.



So how can the script then create each of the subdirectories that are listed in this text file?

I'm completely lost on that part.



This is my code so far:



 echo "Enter the project name "
read name
echo $name

if [ ! -d $name ] then
mkdir $name

else
echo "The project name you entered already exists"
fi

cp /etc/scriptbuilder/str1/structure1.txt /$name
#I know this is wrong
because this would just copy the file over to the new directory but not actually
make the two subdirectories that are on the file onto the new directory









share|improve this question




























    0















    When a user inputs a name, there should be a new directory that gets created under that name.

    In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.

    In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.



    So how can the script then create each of the subdirectories that are listed in this text file?

    I'm completely lost on that part.



    This is my code so far:



     echo "Enter the project name "
    read name
    echo $name

    if [ ! -d $name ] then
    mkdir $name

    else
    echo "The project name you entered already exists"
    fi

    cp /etc/scriptbuilder/str1/structure1.txt /$name
    #I know this is wrong
    because this would just copy the file over to the new directory but not actually
    make the two subdirectories that are on the file onto the new directory









    share|improve this question


























      0












      0








      0








      When a user inputs a name, there should be a new directory that gets created under that name.

      In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.

      In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.



      So how can the script then create each of the subdirectories that are listed in this text file?

      I'm completely lost on that part.



      This is my code so far:



       echo "Enter the project name "
      read name
      echo $name

      if [ ! -d $name ] then
      mkdir $name

      else
      echo "The project name you entered already exists"
      fi

      cp /etc/scriptbuilder/str1/structure1.txt /$name
      #I know this is wrong
      because this would just copy the file over to the new directory but not actually
      make the two subdirectories that are on the file onto the new directory









      share|improve this question
















      When a user inputs a name, there should be a new directory that gets created under that name.

      In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.

      In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.



      So how can the script then create each of the subdirectories that are listed in this text file?

      I'm completely lost on that part.



      This is my code so far:



       echo "Enter the project name "
      read name
      echo $name

      if [ ! -d $name ] then
      mkdir $name

      else
      echo "The project name you entered already exists"
      fi

      cp /etc/scriptbuilder/str1/structure1.txt /$name
      #I know this is wrong
      because this would just copy the file over to the new directory but not actually
      make the two subdirectories that are on the file onto the new directory






      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 9:00









      Adriano

      1,38411527




      1,38411527










      asked Nov 16 '18 at 3:21









      Srk93Srk93

      225




      225






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The bash command that you are looking for is read.

          Also the syntax for your if [ ! -d "$name" ] should have a semicolon.

          The else would typically have an exit 1 (or some such value).

          Typical bash code gets input from the command line, but what you want is fine.



          For testing purposes, I inserted a ~ (tilde), which references your home directory.



          The script should look something like:



          filename="/etc/scriptbuilder/str1"
          read -p "Enter the project name " name
          echo "$name"
          if [ ! -d ~/"$name" ]; then
          mkdir ~/"$name"
          else
          echo "The project name you entered already exists"
          exit 1
          fi
          while read -r line; do
          mkdir ~/"$name/$line"
          done < "$filename"


          You can clean up the formatting.






          share|improve this answer




















          • 2





            All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

            – tripleee
            Nov 16 '18 at 5:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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%2fstackoverflow.com%2fquestions%2f53330946%2fhow-to-get-a-list-of-subdirectories-from-a-file-and-then-create-those-subdirecto%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The bash command that you are looking for is read.

          Also the syntax for your if [ ! -d "$name" ] should have a semicolon.

          The else would typically have an exit 1 (or some such value).

          Typical bash code gets input from the command line, but what you want is fine.



          For testing purposes, I inserted a ~ (tilde), which references your home directory.



          The script should look something like:



          filename="/etc/scriptbuilder/str1"
          read -p "Enter the project name " name
          echo "$name"
          if [ ! -d ~/"$name" ]; then
          mkdir ~/"$name"
          else
          echo "The project name you entered already exists"
          exit 1
          fi
          while read -r line; do
          mkdir ~/"$name/$line"
          done < "$filename"


          You can clean up the formatting.






          share|improve this answer




















          • 2





            All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

            – tripleee
            Nov 16 '18 at 5:29















          1














          The bash command that you are looking for is read.

          Also the syntax for your if [ ! -d "$name" ] should have a semicolon.

          The else would typically have an exit 1 (or some such value).

          Typical bash code gets input from the command line, but what you want is fine.



          For testing purposes, I inserted a ~ (tilde), which references your home directory.



          The script should look something like:



          filename="/etc/scriptbuilder/str1"
          read -p "Enter the project name " name
          echo "$name"
          if [ ! -d ~/"$name" ]; then
          mkdir ~/"$name"
          else
          echo "The project name you entered already exists"
          exit 1
          fi
          while read -r line; do
          mkdir ~/"$name/$line"
          done < "$filename"


          You can clean up the formatting.






          share|improve this answer




















          • 2





            All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

            – tripleee
            Nov 16 '18 at 5:29













          1












          1








          1







          The bash command that you are looking for is read.

          Also the syntax for your if [ ! -d "$name" ] should have a semicolon.

          The else would typically have an exit 1 (or some such value).

          Typical bash code gets input from the command line, but what you want is fine.



          For testing purposes, I inserted a ~ (tilde), which references your home directory.



          The script should look something like:



          filename="/etc/scriptbuilder/str1"
          read -p "Enter the project name " name
          echo "$name"
          if [ ! -d ~/"$name" ]; then
          mkdir ~/"$name"
          else
          echo "The project name you entered already exists"
          exit 1
          fi
          while read -r line; do
          mkdir ~/"$name/$line"
          done < "$filename"


          You can clean up the formatting.






          share|improve this answer















          The bash command that you are looking for is read.

          Also the syntax for your if [ ! -d "$name" ] should have a semicolon.

          The else would typically have an exit 1 (or some such value).

          Typical bash code gets input from the command line, but what you want is fine.



          For testing purposes, I inserted a ~ (tilde), which references your home directory.



          The script should look something like:



          filename="/etc/scriptbuilder/str1"
          read -p "Enter the project name " name
          echo "$name"
          if [ ! -d ~/"$name" ]; then
          mkdir ~/"$name"
          else
          echo "The project name you entered already exists"
          exit 1
          fi
          while read -r line; do
          mkdir ~/"$name/$line"
          done < "$filename"


          You can clean up the formatting.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 5:28









          tripleee

          94.6k13133187




          94.6k13133187










          answered Nov 16 '18 at 4:38









          BillBill

          817717




          817717







          • 2





            All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

            – tripleee
            Nov 16 '18 at 5:29












          • 2





            All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

            – tripleee
            Nov 16 '18 at 5:29







          2




          2





          All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

          – tripleee
          Nov 16 '18 at 5:29





          All this just to reimplement xargs -a /etc/scriptbuilder/str1 -i mkdir -p

          – tripleee
          Nov 16 '18 at 5:29



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53330946%2fhow-to-get-a-list-of-subdirectories-from-a-file-and-then-create-those-subdirecto%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin