Script to mv set of directory recursively

Hi,

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.

Hi,

Yes, bc is available, tested your seq alternative. Looks alright. Will do more testing. Thanks.

$: ./bc.bash
dir_13
dir_12
dir_11
dir_10
dir_9
dir_8
dir_7
dir_6
dir_5
dir_4
1 Like

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.

Run it with bash -x and see what it does.

/bin/bash -x ./v2.bash.02

There is a mistake in my v2.bash.01; parentheses must enforce the right precedence in

while
  (( (di-=1) >= min ))
do

Hi,

v2.bash.01 now has the following

/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:

/sandbox/MadeInGermany$: bash -x ./v2.bash.01
+ dirbase=dir
+ max=15
+ min=4
+ switch=0
+ di=16
+ ((  (di-=1) >= min  ))
+ sdir=dir15
+ [[ ! -d dir15 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir14
+ [[ ! -d dir14 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir13
+ [[ ! -d dir13 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir12
+ [[ ! -d dir12 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir11
+ [[ ! -d dir11 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir10
+ [[ ! -d dir10 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir9
+ [[ ! -d dir9 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir8
+ [[ ! -d dir8 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir7
+ [[ ! -d dir7 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir6
+ [[ ! -d dir6 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir5
+ [[ ! -d dir5 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ sdir=dir4
+ [[ ! -d dir4 ]]
+ continue
+ ((  (di-=1) >= min  ))
+ [[ 0 -ne 0 ]]
/sandbox/MadeInGermany$:
/sandbox/MadeInGermany$: bash -x ./v2.bash.02
+ dirbase=dir
+ max=15
+ min=4
+ switch=0
+ di=16
+ di=15
+ [[ 15 -ge 4 ]]
+ sdir=dir15
+ [[ ! -d dir15 ]]
+ continue
+ di=14
+ [[ 14 -ge 4 ]]
+ sdir=dir14
+ [[ ! -d dir14 ]]
+ continue
+ di=13
+ [[ 13 -ge 4 ]]
+ sdir=dir13
+ [[ ! -d dir13 ]]
+ continue
+ di=12
+ [[ 12 -ge 4 ]]
+ sdir=dir12
+ [[ ! -d dir12 ]]
+ continue
+ di=11
+ [[ 11 -ge 4 ]]
+ sdir=dir11
+ [[ ! -d dir11 ]]
+ continue
+ di=10
+ [[ 10 -ge 4 ]]
+ sdir=dir10
+ [[ ! -d dir10 ]]
+ continue
+ di=9
+ [[ 9 -ge 4 ]]
+ sdir=dir9
+ [[ ! -d dir9 ]]
+ continue
+ di=8
+ [[ 8 -ge 4 ]]
+ sdir=dir8
+ [[ ! -d dir8 ]]
+ continue
+ di=7
+ [[ 7 -ge 4 ]]
+ sdir=dir7
+ [[ ! -d dir7 ]]
+ continue
+ di=6
+ [[ 6 -ge 4 ]]
+ sdir=dir6
+ [[ ! -d dir6 ]]
+ continue
+ di=5
+ [[ 5 -ge 4 ]]
+ sdir=dir5
+ [[ ! -d dir5 ]]
+ continue
+ di=4
+ [[ 4 -ge 4 ]]
+ sdir=dir4
+ [[ ! -d dir4 ]]
+ continue
+ di=3
+ [[ 3 -ge 4 ]]
+ [[ 0 -ne 0 ]]
/sandbox/MadeInGermany$:

None of the dir{4..16} exist.
As I said, it only moves existing dirs.
What do you expect?

Aaahh, gotcha --- I see, silly me --- I created some test dirs based on the sample txt files and yeah, they work fine now. Thanks.