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?
bash -x scriptname
to see what it is doing.