Issue with Bash Script creating Directories from Arrays

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











up vote
3
down vote

favorite
1












Issue at hand



I am trying to write a bash script to quickly create a directory structure. This is an attempt to learn more about manipulating arrays, variables, and using loops. My script works to check for the existence of a directory then create folders. The issue I am having is creating a third level of directories within the first two layers.



Goals



I want to be able to write a bash script that will create a directory structure of ~/a/a/a, ~/a/a/b, ~/a/a/c,...,~/a/z/z for example. This should be flexible to so I could use any kind of array or variable that would be suitable.



Here is what I have worked out so far:



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

if [ ! -d "directory" ]; then
mkdir directory
fi
for array_0 in "$array_0[@]"
do
mkdir ~/directory/$array_0/
done
if [ -d "~/directory/$array_0/" ]; then
for array_1 in "$array_0[@]"
do
mkdir ~/directory/$array_0/$array_1
done
fi
exit 0


Problem



The error I get is mkdir: cannot create directory '/home/user/directory/one/x' : No such file or directory



Other attempts at this script allow me to create ~/directory and ~/directory/one, ~/directory/two,..., ~/directory/five without fail but not the next level i.e /directory/one/x and etc.



How can I script the creation of this directory structure? Is this possible using arrays or is there another method?



For reference I tried to implement this post and elements from this post but I have not had any luck creating the directory structure that I want.







share|improve this question


















  • 2




    mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
    – Jesse_b
    Feb 26 at 20:33











  • for array_0 in "$array_0[@]" Oops.
    – Ignacio Vazquez-Abrams
    Feb 26 at 20:33










  • @ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
    – kemotep
    Feb 26 at 20:43






  • 1




    @kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
    – ilkkachu
    Feb 26 at 22:04










  • This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
    – Scott
    Apr 3 at 17:21














up vote
3
down vote

favorite
1












Issue at hand



I am trying to write a bash script to quickly create a directory structure. This is an attempt to learn more about manipulating arrays, variables, and using loops. My script works to check for the existence of a directory then create folders. The issue I am having is creating a third level of directories within the first two layers.



Goals



I want to be able to write a bash script that will create a directory structure of ~/a/a/a, ~/a/a/b, ~/a/a/c,...,~/a/z/z for example. This should be flexible to so I could use any kind of array or variable that would be suitable.



Here is what I have worked out so far:



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

if [ ! -d "directory" ]; then
mkdir directory
fi
for array_0 in "$array_0[@]"
do
mkdir ~/directory/$array_0/
done
if [ -d "~/directory/$array_0/" ]; then
for array_1 in "$array_0[@]"
do
mkdir ~/directory/$array_0/$array_1
done
fi
exit 0


Problem



The error I get is mkdir: cannot create directory '/home/user/directory/one/x' : No such file or directory



Other attempts at this script allow me to create ~/directory and ~/directory/one, ~/directory/two,..., ~/directory/five without fail but not the next level i.e /directory/one/x and etc.



How can I script the creation of this directory structure? Is this possible using arrays or is there another method?



For reference I tried to implement this post and elements from this post but I have not had any luck creating the directory structure that I want.







share|improve this question


















  • 2




    mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
    – Jesse_b
    Feb 26 at 20:33











  • for array_0 in "$array_0[@]" Oops.
    – Ignacio Vazquez-Abrams
    Feb 26 at 20:33










  • @ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
    – kemotep
    Feb 26 at 20:43






  • 1




    @kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
    – ilkkachu
    Feb 26 at 22:04










  • This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
    – Scott
    Apr 3 at 17:21












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





Issue at hand



I am trying to write a bash script to quickly create a directory structure. This is an attempt to learn more about manipulating arrays, variables, and using loops. My script works to check for the existence of a directory then create folders. The issue I am having is creating a third level of directories within the first two layers.



Goals



I want to be able to write a bash script that will create a directory structure of ~/a/a/a, ~/a/a/b, ~/a/a/c,...,~/a/z/z for example. This should be flexible to so I could use any kind of array or variable that would be suitable.



Here is what I have worked out so far:



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

if [ ! -d "directory" ]; then
mkdir directory
fi
for array_0 in "$array_0[@]"
do
mkdir ~/directory/$array_0/
done
if [ -d "~/directory/$array_0/" ]; then
for array_1 in "$array_0[@]"
do
mkdir ~/directory/$array_0/$array_1
done
fi
exit 0


Problem



The error I get is mkdir: cannot create directory '/home/user/directory/one/x' : No such file or directory



Other attempts at this script allow me to create ~/directory and ~/directory/one, ~/directory/two,..., ~/directory/five without fail but not the next level i.e /directory/one/x and etc.



How can I script the creation of this directory structure? Is this possible using arrays or is there another method?



For reference I tried to implement this post and elements from this post but I have not had any luck creating the directory structure that I want.







share|improve this question














Issue at hand



I am trying to write a bash script to quickly create a directory structure. This is an attempt to learn more about manipulating arrays, variables, and using loops. My script works to check for the existence of a directory then create folders. The issue I am having is creating a third level of directories within the first two layers.



Goals



I want to be able to write a bash script that will create a directory structure of ~/a/a/a, ~/a/a/b, ~/a/a/c,...,~/a/z/z for example. This should be flexible to so I could use any kind of array or variable that would be suitable.



Here is what I have worked out so far:



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

if [ ! -d "directory" ]; then
mkdir directory
fi
for array_0 in "$array_0[@]"
do
mkdir ~/directory/$array_0/
done
if [ -d "~/directory/$array_0/" ]; then
for array_1 in "$array_0[@]"
do
mkdir ~/directory/$array_0/$array_1
done
fi
exit 0


Problem



The error I get is mkdir: cannot create directory '/home/user/directory/one/x' : No such file or directory



Other attempts at this script allow me to create ~/directory and ~/directory/one, ~/directory/two,..., ~/directory/five without fail but not the next level i.e /directory/one/x and etc.



How can I script the creation of this directory structure? Is this possible using arrays or is there another method?



For reference I tried to implement this post and elements from this post but I have not had any luck creating the directory structure that I want.









share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 20:42

























asked Feb 26 at 20:32









kemotep

1,0941616




1,0941616







  • 2




    mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
    – Jesse_b
    Feb 26 at 20:33











  • for array_0 in "$array_0[@]" Oops.
    – Ignacio Vazquez-Abrams
    Feb 26 at 20:33










  • @ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
    – kemotep
    Feb 26 at 20:43






  • 1




    @kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
    – ilkkachu
    Feb 26 at 22:04










  • This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
    – Scott
    Apr 3 at 17:21












  • 2




    mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
    – Jesse_b
    Feb 26 at 20:33











  • for array_0 in "$array_0[@]" Oops.
    – Ignacio Vazquez-Abrams
    Feb 26 at 20:33










  • @ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
    – kemotep
    Feb 26 at 20:43






  • 1




    @kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
    – ilkkachu
    Feb 26 at 22:04










  • This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
    – Scott
    Apr 3 at 17:21







2




2




mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
– Jesse_b
Feb 26 at 20:33





mkdir -p will create all required directories. For example you can do mkdir -p one/two/three/four/five and it will create all the directories required (if they don't exist) in order to make five
– Jesse_b
Feb 26 at 20:33













for array_0 in "$array_0[@]" Oops.
– Ignacio Vazquez-Abrams
Feb 26 at 20:33




for array_0 in "$array_0[@]" Oops.
– Ignacio Vazquez-Abrams
Feb 26 at 20:33












@ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
– kemotep
Feb 26 at 20:43




@ikkachu I posted the error, also that reference was a mistake. The script you see if the actual script this is simply an exercise in learning bash scripts.
– kemotep
Feb 26 at 20:43




1




1




@kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
– ilkkachu
Feb 26 at 22:04




@kemotep, the script has x in array_1[0], but the first time array_1 is referenced after the assignment is when array_1 is used to loop over the values of array_0. So no, it doesn't look like that script would try to create directory/one/x, or otherwise lead to that error...
– ilkkachu
Feb 26 at 22:04












This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
– Scott
Apr 3 at 17:21




This question has multiple problems.  It is, of course, OK to post non-functioning scripts in questions, but it is not OK to post one version of a script along with an error message that comes from a different (unpublished) version of the script.
– Scott
Apr 3 at 17:21










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You could use a nested array loop, like this



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

for a0 in "$array_0[@]"
do
for a1 in "$array_1[@]"
do
mkdir -p "$HOME/web/$a0/$a1"
done
done


Or, if you don't mind avoiding the use of arrays but using expansion lists instead, this single command will do much the same thing:



mkdir -p ~/one,two,three,four,five/x,y,z





share|improve this answer






















  • I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
    – kemotep
    Feb 26 at 20:48










  • @kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
    – roaima
    Feb 26 at 21:40










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f426791%2fissue-with-bash-script-creating-directories-from-arrays%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










You could use a nested array loop, like this



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

for a0 in "$array_0[@]"
do
for a1 in "$array_1[@]"
do
mkdir -p "$HOME/web/$a0/$a1"
done
done


Or, if you don't mind avoiding the use of arrays but using expansion lists instead, this single command will do much the same thing:



mkdir -p ~/one,two,three,four,five/x,y,z





share|improve this answer






















  • I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
    – kemotep
    Feb 26 at 20:48










  • @kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
    – roaima
    Feb 26 at 21:40














up vote
2
down vote



accepted










You could use a nested array loop, like this



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

for a0 in "$array_0[@]"
do
for a1 in "$array_1[@]"
do
mkdir -p "$HOME/web/$a0/$a1"
done
done


Or, if you don't mind avoiding the use of arrays but using expansion lists instead, this single command will do much the same thing:



mkdir -p ~/one,two,three,four,five/x,y,z





share|improve this answer






















  • I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
    – kemotep
    Feb 26 at 20:48










  • @kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
    – roaima
    Feb 26 at 21:40












up vote
2
down vote



accepted







up vote
2
down vote



accepted






You could use a nested array loop, like this



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

for a0 in "$array_0[@]"
do
for a1 in "$array_1[@]"
do
mkdir -p "$HOME/web/$a0/$a1"
done
done


Or, if you don't mind avoiding the use of arrays but using expansion lists instead, this single command will do much the same thing:



mkdir -p ~/one,two,three,four,five/x,y,z





share|improve this answer














You could use a nested array loop, like this



#!/bin/bash
array_0=(one two three four five)
array_1=(x y z)

for a0 in "$array_0[@]"
do
for a1 in "$array_1[@]"
do
mkdir -p "$HOME/web/$a0/$a1"
done
done


Or, if you don't mind avoiding the use of arrays but using expansion lists instead, this single command will do much the same thing:



mkdir -p ~/one,two,three,four,five/x,y,z






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 26 at 21:38

























answered Feb 26 at 20:38









roaima

39.5k545107




39.5k545107











  • I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
    – kemotep
    Feb 26 at 20:48










  • @kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
    – roaima
    Feb 26 at 21:40
















  • I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
    – kemotep
    Feb 26 at 20:48










  • @kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
    – roaima
    Feb 26 at 21:40















I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
– kemotep
Feb 26 at 20:48




I did not know you could do this in a one-liner. I just learned of arrays and thought that they would help. Your mkdir -p ~/one,two,three,four,five/x,y,z example works great. Thank you, I was making this too complicated.
– kemotep
Feb 26 at 20:48












@kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
– roaima
Feb 26 at 21:40




@kemotep there's definitely a time and place for arrays so don't worry about this not necessarily being one of them.
– roaima
Feb 26 at 21:40












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426791%2fissue-with-bash-script-creating-directories-from-arrays%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?