Making Directories with Bash Script

Clash 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?
command-line bash scripts directory
add a comment |Â
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?
command-line bash scripts directory
2
Run your script withbash -x scriptnameto see what it is doing.
â waltinator
May 14 at 17:22
add a comment |Â
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?
command-line bash scripts directory
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?
command-line bash scripts directory
asked May 14 at 17:07
David Ollodart
83
83
2
Run your script withbash -x scriptnameto see what it is doing.
â waltinator
May 14 at 17:22
add a comment |Â
2
Run your script withbash -x scriptnameto 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
add a comment |Â
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, wheredump/exists dump/var1is created- you
cdintodump/var1. - when
cd ..occurs you go back todump/. Current working directory isdumpand there's onlyvar1there, nothing else.
On the second iteration:
- When you try
mkdir dump/var2/you are indump/and there's onlyvar1there. Thedump/ofdump/var2is the non-existent path. Of course it fails. Hence the error message and hence the duplicatedump/dump/var2when you usemkdir -pflag. - Your script still does
cd .., so before 3rd iteration current working directory changes fromdump/todump/'s parent.
For 3rd iteration:
- You're inside
dump/'s parent directory, the pathdump/exists, hencemkdir dump/var3won't fail. cd dump/var3occurs, thencd .., and what is the current working directory now ?dump/, the one level abovedump/var3, where you try to domkdir dump/dump/var4, but there's onlyvar1andvar3there, nodump. Themkdirfails, youcd ..which goes one level abovedump/, 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.
add a comment |Â
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, wheredump/exists dump/var1is created- you
cdintodump/var1. - when
cd ..occurs you go back todump/. Current working directory isdumpand there's onlyvar1there, nothing else.
On the second iteration:
- When you try
mkdir dump/var2/you are indump/and there's onlyvar1there. Thedump/ofdump/var2is the non-existent path. Of course it fails. Hence the error message and hence the duplicatedump/dump/var2when you usemkdir -pflag. - Your script still does
cd .., so before 3rd iteration current working directory changes fromdump/todump/'s parent.
For 3rd iteration:
- You're inside
dump/'s parent directory, the pathdump/exists, hencemkdir dump/var3won't fail. cd dump/var3occurs, thencd .., and what is the current working directory now ?dump/, the one level abovedump/var3, where you try to domkdir dump/dump/var4, but there's onlyvar1andvar3there, nodump. Themkdirfails, youcd ..which goes one level abovedump/, 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.
add a comment |Â
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, wheredump/exists dump/var1is created- you
cdintodump/var1. - when
cd ..occurs you go back todump/. Current working directory isdumpand there's onlyvar1there, nothing else.
On the second iteration:
- When you try
mkdir dump/var2/you are indump/and there's onlyvar1there. Thedump/ofdump/var2is the non-existent path. Of course it fails. Hence the error message and hence the duplicatedump/dump/var2when you usemkdir -pflag. - Your script still does
cd .., so before 3rd iteration current working directory changes fromdump/todump/'s parent.
For 3rd iteration:
- You're inside
dump/'s parent directory, the pathdump/exists, hencemkdir dump/var3won't fail. cd dump/var3occurs, thencd .., and what is the current working directory now ?dump/, the one level abovedump/var3, where you try to domkdir dump/dump/var4, but there's onlyvar1andvar3there, nodump. Themkdirfails, youcd ..which goes one level abovedump/, 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.
add a comment |Â
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, wheredump/exists dump/var1is created- you
cdintodump/var1. - when
cd ..occurs you go back todump/. Current working directory isdumpand there's onlyvar1there, nothing else.
On the second iteration:
- When you try
mkdir dump/var2/you are indump/and there's onlyvar1there. Thedump/ofdump/var2is the non-existent path. Of course it fails. Hence the error message and hence the duplicatedump/dump/var2when you usemkdir -pflag. - Your script still does
cd .., so before 3rd iteration current working directory changes fromdump/todump/'s parent.
For 3rd iteration:
- You're inside
dump/'s parent directory, the pathdump/exists, hencemkdir dump/var3won't fail. cd dump/var3occurs, thencd .., and what is the current working directory now ?dump/, the one level abovedump/var3, where you try to domkdir dump/dump/var4, but there's onlyvar1andvar3there, nodump. Themkdirfails, youcd ..which goes one level abovedump/, 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.
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, wheredump/exists dump/var1is created- you
cdintodump/var1. - when
cd ..occurs you go back todump/. Current working directory isdumpand there's onlyvar1there, nothing else.
On the second iteration:
- When you try
mkdir dump/var2/you are indump/and there's onlyvar1there. Thedump/ofdump/var2is the non-existent path. Of course it fails. Hence the error message and hence the duplicatedump/dump/var2when you usemkdir -pflag. - Your script still does
cd .., so before 3rd iteration current working directory changes fromdump/todump/'s parent.
For 3rd iteration:
- You're inside
dump/'s parent directory, the pathdump/exists, hencemkdir dump/var3won't fail. cd dump/var3occurs, thencd .., and what is the current working directory now ?dump/, the one level abovedump/var3, where you try to domkdir dump/dump/var4, but there's onlyvar1andvar3there, nodump. Themkdirfails, youcd ..which goes one level abovedump/, 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.
edited May 14 at 17:48
answered May 14 at 17:25
Sergiy Kolodyazhnyy
64k9127275
64k9127275
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Run your script with
bash -x scriptnameto see what it is doing.â waltinator
May 14 at 17:22