I made the changes that you suggested but unsure if I did it right or not as it doesn't appear to work.
v2.bash.00 is the original one that uses the for loop that gives error. 01 and 02 are the while loop that you suggested. Note that this is not using the sample files as you mentioned they are not needed as you mentioned.
/sandbox/MadeInGermany$: head -50 v2.bash.00 v2.bash.01 v2.bash.02
==> v2.bash.00 <==
#!/bin/bash
dirbase="dir"
max=15
min=4
switch=0
for (( di=max; di >= min; di-=1 ))
do
# Source dir must exist
sdir="$dirbase$di"
if [[ ! -d "$sdir" ]] ; then
continue
fi
# Increment or delete the first (highest) dir
if [[ $switch -eq 0 ]] ; then
if [[ $di -ne $max ]] ; then
echo mv "$sdir" "$dirbase$(( di + 1 ))"
else
echo rm -r "$sdir"
fi
switch=1
else
echo mv "$sdir" "$pdir"
fi
pdir=$sdir
done #di
# Recreate the last (lowest) dir
if [[ $switch -ne 0 ]] ; then
echo mkdir "$pdir"
fi
==> v2.bash.01 <==
#!/bin/bash
dirbase="dir"
max=15
min=4
switch=0
di=$(( max + 1 ))
while
(( di-=1 >= min ))
do
# Source dir must exist
sdir="$dirbase$di"
if [[ ! -d "$sdir" ]] ; then
continue
fi
# Increment or delete the first (highest) dir
if [[ $switch -eq 0 ]] ; then
if [[ $di -ne $max ]] ; then
echo mv "$sdir" "$dirbase$(( di + 1 ))"
else
echo rm -r "$sdir"
fi
switch=1
else
echo mv "$sdir" "$pdir"
fi
pdir=$sdir
done #di
# Recreate the last (lowest) dir
if [[ $switch -ne 0 ]] ; then
echo mkdir "$pdir"
fi
==> v2.bash.02 <==
#!/bin/bash
dirbase="dir"
max=15
min=4
switch=0
di=$(( max + 1 ))
while
di=$(( di - 1 ))
[[ $di -ge $min ]]
do
# Source dir must exist
sdir="$dirbase$di"
if [[ ! -d "$sdir" ]] ; then
continue
fi
# Increment or delete the first (highest) dir
if [[ $switch -eq 0 ]] ; then
if [[ $di -ne $max ]] ; then
echo mv "$sdir" "$dirbase$(( di + 1 ))"
else
echo rm -r "$sdir"
fi
switch=1
else
echo mv "$sdir" "$pdir"
fi
pdir=$sdir
done #di
# Recreate the last (lowest) dir
if [[ $switch -ne 0 ]] ; then
echo mkdir "$pdir"
fi
Running each version, the 00 version gives error as expected. 01 version just hangs there, I have to press CTRL-C eventually. 02 runs but doesn't do anything, it just return to the command prompt immediately.
/sandbox/MadeInGermany$: ./v2.bash.00
./v2.bash.00: line 8: syntax error near unexpected token `(('
./v2.bash.00: line 8: `for (( di=max; di >= min; di-=1 ))'
/sandbox/MadeInGermany$: ./v2.bash.01
^C
/sandbox/MadeInGermany$: ./v2.bash.02
/sandbox/MadeInGermany$:
/sandbox/MadeInGermany$: bash --version
GNU bash, version 2.03.0(3)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.
At the moment, there is another job that creates the missing directory. I will check if we can disable that job and then we just create the missing directory when doing the mv. At the moment, the missing directory that has to be created is the last directory, the min, as we know it will 'disappear' during the mv.
/sandbox/MadeInGermany$: head -15 v2.bash.01
#!/bin/bash
dirbase="dir"
max=15
min=4
switch=0
di=$(( max + 1 ))
while
(( (di-=1) >= min ))
do
# Source dir must exist
sdir="$dirbase$di"
if [[ ! -d "$sdir" ]] ; then
continue
v2.bash.01 run is not hang now but it prints nothing, similar to 02, bash -x run for both as below: