Making Directories with Bash Script

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








up vote
1
down vote

favorite












I have the following bash script to make a set of directories, move to that directory, execute some actions (not shown), and move back up to the parent directory:



#!/bin/bash
if [ -e 'dump' ]; then
rm -r dump
mkdir dump
D="dump/var"
else
mkdir dump
D="dump/var"
fi

for d in 1..24

do
echo $D$d
mkdir $D$d/
cd $D$d
cd ..

done


When this script is executed, the result is that odd-numbered directories are made while even-numbered directories throw the error 'No such file or directory'. If I put in the option to make a parent directory, as in mkdir -p $D$d/, a nested directory results dump/var1, dump/dump/var2, dump/dump/dump/var3, et cetera.



If I remove the directory changes, cd $D$d and cd .., then the script executes without error. If I use absolute paths for the directory, as in D="/path/to/directory/dump/var", then there is 'No such file or directory' for all but the first directory created.



What is happening in the shell?







share|improve this question
















  • 2




    Run your script with bash -x scriptname to see what it is doing.
    – waltinator
    May 14 at 17:22














up vote
1
down vote

favorite












I have the following bash script to make a set of directories, move to that directory, execute some actions (not shown), and move back up to the parent directory:



#!/bin/bash
if [ -e 'dump' ]; then
rm -r dump
mkdir dump
D="dump/var"
else
mkdir dump
D="dump/var"
fi

for d in 1..24

do
echo $D$d
mkdir $D$d/
cd $D$d
cd ..

done


When this script is executed, the result is that odd-numbered directories are made while even-numbered directories throw the error 'No such file or directory'. If I put in the option to make a parent directory, as in mkdir -p $D$d/, a nested directory results dump/var1, dump/dump/var2, dump/dump/dump/var3, et cetera.



If I remove the directory changes, cd $D$d and cd .., then the script executes without error. If I use absolute paths for the directory, as in D="/path/to/directory/dump/var", then there is 'No such file or directory' for all but the first directory created.



What is happening in the shell?







share|improve this question
















  • 2




    Run your script with bash -x scriptname to see what it is doing.
    – waltinator
    May 14 at 17:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following bash script to make a set of directories, move to that directory, execute some actions (not shown), and move back up to the parent directory:



#!/bin/bash
if [ -e 'dump' ]; then
rm -r dump
mkdir dump
D="dump/var"
else
mkdir dump
D="dump/var"
fi

for d in 1..24

do
echo $D$d
mkdir $D$d/
cd $D$d
cd ..

done


When this script is executed, the result is that odd-numbered directories are made while even-numbered directories throw the error 'No such file or directory'. If I put in the option to make a parent directory, as in mkdir -p $D$d/, a nested directory results dump/var1, dump/dump/var2, dump/dump/dump/var3, et cetera.



If I remove the directory changes, cd $D$d and cd .., then the script executes without error. If I use absolute paths for the directory, as in D="/path/to/directory/dump/var", then there is 'No such file or directory' for all but the first directory created.



What is happening in the shell?







share|improve this question












I have the following bash script to make a set of directories, move to that directory, execute some actions (not shown), and move back up to the parent directory:



#!/bin/bash
if [ -e 'dump' ]; then
rm -r dump
mkdir dump
D="dump/var"
else
mkdir dump
D="dump/var"
fi

for d in 1..24

do
echo $D$d
mkdir $D$d/
cd $D$d
cd ..

done


When this script is executed, the result is that odd-numbered directories are made while even-numbered directories throw the error 'No such file or directory'. If I put in the option to make a parent directory, as in mkdir -p $D$d/, a nested directory results dump/var1, dump/dump/var2, dump/dump/dump/var3, et cetera.



If I remove the directory changes, cd $D$d and cd .., then the script executes without error. If I use absolute paths for the directory, as in D="/path/to/directory/dump/var", then there is 'No such file or directory' for all but the first directory created.



What is happening in the shell?









share|improve this question











share|improve this question




share|improve this question










asked May 14 at 17:07









David Ollodart

83




83







  • 2




    Run your script with bash -x scriptname to see what it is doing.
    – waltinator
    May 14 at 17:22












  • 2




    Run your script with bash -x scriptname to see what it is doing.
    – waltinator
    May 14 at 17:22







2




2




Run your script with bash -x scriptname to see what it is doing.
– waltinator
May 14 at 17:22




Run your script with bash -x scriptname to see what it is doing.
– waltinator
May 14 at 17:22










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Core of the issue: your script cd back to dump, not to dump/'s parent.



On first iteration:



  • You start out in dump's parent directory, where dump/ exists


  • dump/var1 is created

  • you cd into dump/var1.

  • when cd .. occurs you go back to dump/. Current working directory is dump and there's only var1 there, nothing else.

On the second iteration:



  • When you try mkdir dump/var2/ you are in dump/ and there's only var1 there. The dump/ of dump/var2 is the non-existent path. Of course it fails. Hence the error message and hence the duplicate dump/dump/var2 when you use mkdir -p flag.

  • Your script still does cd .., so before 3rd iteration current working directory changes from dump/ to dump/'s parent.

For 3rd iteration:



  • You're inside dump/'s parent directory, the path dump/ exists, hence mkdir dump/var3 won't fail.


  • cd dump/var3 occurs, then cd .., and what is the current working directory now ? dump/ , the one level above dump/var3, where you try to do mkdir dump/dump/var4, but there's only var1 and var3 there, no dump. The mkdir fails, you cd .. which goes one level above dump/, and the whole thing repeats again.

You can see this clearly when you execute our script with set -x for debugging output appended after #!/bin/bash line or by doing pwd as first command on each iteration of the script.



The way I'd recommend fixing your script is via cd into dump/ first, and then run the for loop. In other words:



cd "$D"
for i in 1..24
do
mkdir "$i"
cd "$i"
# do whatever you want inside "$i"
cd ..
done


Among other things, I'd suggest using [ -d "dump" ] instead of -e, because that'll help you make sure the file that exists is actually the directory, although this might take extra considerations.






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%2f1036194%2fmaking-directories-with-bash-script%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
    2
    down vote



    accepted










    Core of the issue: your script cd back to dump, not to dump/'s parent.



    On first iteration:



    • You start out in dump's parent directory, where dump/ exists


    • dump/var1 is created

    • you cd into dump/var1.

    • when cd .. occurs you go back to dump/. Current working directory is dump and there's only var1 there, nothing else.

    On the second iteration:



    • When you try mkdir dump/var2/ you are in dump/ and there's only var1 there. The dump/ of dump/var2 is the non-existent path. Of course it fails. Hence the error message and hence the duplicate dump/dump/var2 when you use mkdir -p flag.

    • Your script still does cd .., so before 3rd iteration current working directory changes from dump/ to dump/'s parent.

    For 3rd iteration:



    • You're inside dump/'s parent directory, the path dump/ exists, hence mkdir dump/var3 won't fail.


    • cd dump/var3 occurs, then cd .., and what is the current working directory now ? dump/ , the one level above dump/var3, where you try to do mkdir dump/dump/var4, but there's only var1 and var3 there, no dump. The mkdir fails, you cd .. which goes one level above dump/, and the whole thing repeats again.

    You can see this clearly when you execute our script with set -x for debugging output appended after #!/bin/bash line or by doing pwd as first command on each iteration of the script.



    The way I'd recommend fixing your script is via cd into dump/ first, and then run the for loop. In other words:



    cd "$D"
    for i in 1..24
    do
    mkdir "$i"
    cd "$i"
    # do whatever you want inside "$i"
    cd ..
    done


    Among other things, I'd suggest using [ -d "dump" ] instead of -e, because that'll help you make sure the file that exists is actually the directory, although this might take extra considerations.






    share|improve this answer


























      up vote
      2
      down vote



      accepted










      Core of the issue: your script cd back to dump, not to dump/'s parent.



      On first iteration:



      • You start out in dump's parent directory, where dump/ exists


      • dump/var1 is created

      • you cd into dump/var1.

      • when cd .. occurs you go back to dump/. Current working directory is dump and there's only var1 there, nothing else.

      On the second iteration:



      • When you try mkdir dump/var2/ you are in dump/ and there's only var1 there. The dump/ of dump/var2 is the non-existent path. Of course it fails. Hence the error message and hence the duplicate dump/dump/var2 when you use mkdir -p flag.

      • Your script still does cd .., so before 3rd iteration current working directory changes from dump/ to dump/'s parent.

      For 3rd iteration:



      • You're inside dump/'s parent directory, the path dump/ exists, hence mkdir dump/var3 won't fail.


      • cd dump/var3 occurs, then cd .., and what is the current working directory now ? dump/ , the one level above dump/var3, where you try to do mkdir dump/dump/var4, but there's only var1 and var3 there, no dump. The mkdir fails, you cd .. which goes one level above dump/, and the whole thing repeats again.

      You can see this clearly when you execute our script with set -x for debugging output appended after #!/bin/bash line or by doing pwd as first command on each iteration of the script.



      The way I'd recommend fixing your script is via cd into dump/ first, and then run the for loop. In other words:



      cd "$D"
      for i in 1..24
      do
      mkdir "$i"
      cd "$i"
      # do whatever you want inside "$i"
      cd ..
      done


      Among other things, I'd suggest using [ -d "dump" ] instead of -e, because that'll help you make sure the file that exists is actually the directory, although this might take extra considerations.






      share|improve this answer
























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Core of the issue: your script cd back to dump, not to dump/'s parent.



        On first iteration:



        • You start out in dump's parent directory, where dump/ exists


        • dump/var1 is created

        • you cd into dump/var1.

        • when cd .. occurs you go back to dump/. Current working directory is dump and there's only var1 there, nothing else.

        On the second iteration:



        • When you try mkdir dump/var2/ you are in dump/ and there's only var1 there. The dump/ of dump/var2 is the non-existent path. Of course it fails. Hence the error message and hence the duplicate dump/dump/var2 when you use mkdir -p flag.

        • Your script still does cd .., so before 3rd iteration current working directory changes from dump/ to dump/'s parent.

        For 3rd iteration:



        • You're inside dump/'s parent directory, the path dump/ exists, hence mkdir dump/var3 won't fail.


        • cd dump/var3 occurs, then cd .., and what is the current working directory now ? dump/ , the one level above dump/var3, where you try to do mkdir dump/dump/var4, but there's only var1 and var3 there, no dump. The mkdir fails, you cd .. which goes one level above dump/, and the whole thing repeats again.

        You can see this clearly when you execute our script with set -x for debugging output appended after #!/bin/bash line or by doing pwd as first command on each iteration of the script.



        The way I'd recommend fixing your script is via cd into dump/ first, and then run the for loop. In other words:



        cd "$D"
        for i in 1..24
        do
        mkdir "$i"
        cd "$i"
        # do whatever you want inside "$i"
        cd ..
        done


        Among other things, I'd suggest using [ -d "dump" ] instead of -e, because that'll help you make sure the file that exists is actually the directory, although this might take extra considerations.






        share|improve this answer














        Core of the issue: your script cd back to dump, not to dump/'s parent.



        On first iteration:



        • You start out in dump's parent directory, where dump/ exists


        • dump/var1 is created

        • you cd into dump/var1.

        • when cd .. occurs you go back to dump/. Current working directory is dump and there's only var1 there, nothing else.

        On the second iteration:



        • When you try mkdir dump/var2/ you are in dump/ and there's only var1 there. The dump/ of dump/var2 is the non-existent path. Of course it fails. Hence the error message and hence the duplicate dump/dump/var2 when you use mkdir -p flag.

        • Your script still does cd .., so before 3rd iteration current working directory changes from dump/ to dump/'s parent.

        For 3rd iteration:



        • You're inside dump/'s parent directory, the path dump/ exists, hence mkdir dump/var3 won't fail.


        • cd dump/var3 occurs, then cd .., and what is the current working directory now ? dump/ , the one level above dump/var3, where you try to do mkdir dump/dump/var4, but there's only var1 and var3 there, no dump. The mkdir fails, you cd .. which goes one level above dump/, and the whole thing repeats again.

        You can see this clearly when you execute our script with set -x for debugging output appended after #!/bin/bash line or by doing pwd as first command on each iteration of the script.



        The way I'd recommend fixing your script is via cd into dump/ first, and then run the for loop. In other words:



        cd "$D"
        for i in 1..24
        do
        mkdir "$i"
        cd "$i"
        # do whatever you want inside "$i"
        cd ..
        done


        Among other things, I'd suggest using [ -d "dump" ] instead of -e, because that'll help you make sure the file that exists is actually the directory, although this might take extra considerations.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 14 at 17:48

























        answered May 14 at 17:25









        Sergiy Kolodyazhnyy

        64k9127275




        64k9127275






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036194%2fmaking-directories-with-bash-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Unable to execute new pre-installation script (/var/lib/dpkg/tmp.ci/preinst)

            Running the scala interactive shell from the command line

            Do not install recommended packages of dependencies