Home > database >  How can I add conditions for copying
How can I add conditions for copying

Time:06-15

I am trying to copy SQL files.

The filenames are versions:

001.000.000.000001.sql

I do have to current DB Version in a variable. The string with the version looks like this:

2002000000020

I don't want to copy all of the files. I just want to copy the files with a version higher than the DB currently has.

I tried to do this with a for loop and an if-statement, but realized quickly that this is slow.

My first approach looked like this:

for d in $archive_deltascripts_dir/*; do
  for f in $d/*; do
    local filename=$(basename "$f")
    cp -r "$f" "$path"
  done
done

for f in $release_deltascripts_dir/*; do
  local filename=$(basename "$f")
  cp -r "$f" "$path"
done

Currently I'm just copying all files without loop:

for d in $archive_deltascripts_dir/*; do
  cp -r "$d/." "$path"
done
cp -r "$release_deltascripts_dir/." "$path"

Is there anyway to add conditions which files should be copied without having to use a for-loop?

I can convert the version in the filename to the same syntax the Database has:

echo ${scriptName} | sed 's/\(^[0-9]\{1\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{6\}\).*$/\1/g' | sed 's/\.//g'`

CodePudding user response:

try:

ls |awk -F '.' '($1 >= '$version1' && $2 >= '$version2' && $3 >= '$version3' && $4 >= '$version4')' |xargs -I {} cp {} /path/target 

I am not sure what's the relation between 2002000000020 and 001.000.000.000001. Maybe you need

version=001000000000001
version1=`echo $version |cut -c-3`
version2=`echo $version |cut -c4-6`
version3=`echo $version |cut -c7-9`
version4=`echo $version |cut -c10-`
  • Related