简体   繁体   中英

batch file to read file contents and iterate

I am trying to write a batch file/shell script that reads the contents of a file, matches those contents with a directory structure and invokes an executable file.

Let's say there is a sequence.txt file in /system/ directory. The sequence file is to represent or force the order of execution. This is important

The sequence.txt file has following enteries:

1;schema1;procedures
1;schema1;functions
2;schema2;procedures
2;schema2;functions
........

and then there is a directory, and the directory has following subdirs

/scripts
|  +--/schema1
|  |  +--/procedures
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/functions
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/packages
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/logs
|  +--/schema2
|  |  +--/procedures
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/functions
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/packages
|  |  |  --1.sql
|  |  |  --2.sql
|  |  +--/logs
.......
........

Now I would like to run flyway (a database migration software) in a loop using this way:

Flyway -schema=schema1 -locations=/scripts/schema1/procedures, /scripts/schema1/functions, /scripts/schema1/packages migrate -x | tee /scripts/schema1/log/logfile_ddmmyy.log

Flyway -schema=schema2 -locations=/scripts/schema2/procedures, /scripts/schema2/functions, /scripts/schema2/packages migrate -x | tee /scripts/schema2/log/logfile_ddmmyy.log

So far this is my progress:

#!/bin/bash
while read i; 
    do echo "$i"; 
done < ./system/sequence.txt

How can I proceed further? I know that this kind of scripting involves variables and then loops but I can't find a way to translate it into technical level.

Cheers and thanks in advance! :-)

I am not sure about the exact relation betwen values and final command path but awk is the tool to construct the call. Use somethin like:

c = `echo $i | awk -F ";" '{print "flyway" $1 "_" $2}' 

where $x is the position of the value and you can construct a string.

After that you can run the c var with

`echo $c`

That should work.

UPDATED:

If I understand correctly what you need, you have to set two whiles, one inside the other. Something like this:

cat tt.txt | awk -F";" '{print $1}'| sort -u | while read i
do
 sc = `grep $i tt.txt | head -n 1 |  awk -F";" '{print $2}'`
 pt1 = "Flyway -schema=" $sc " -locations="
 pt3 = " -x | tee /scripts/" $sc "/log/logfile_ddmmyy.log"
 grep $i tt.txt | while read j
 do
  c=`echo $j | awk -F";" '{print $2}'| sort -u`
  pt2 = $pt2 "/scripts/" $sc "/" $c ""
 done
 echo $pt1 $pt2 $pt3
done

Though not completely clear about what you want, here is some inspiration:

while read line; do
    OIFS=$IFS
    IFS=';'
    a=()
    for name in ${line}; do
        a+=(${name})
    done
    IFS=$OIFS
    number=${a[0]}
    schema=${a[1]}
    subdir=${a[2]}
    echo "Flyway -schema=${schema} -locations=/scripts/${schema}/procedures, /scripts/${schema}/functions, /scripts/${schema}/packages migrate -x | tee /scripts/${schema}/log/logfile_ddmmyy.log"
done <<EOF
1;schema1;procedures
1;schema1;functions
2;schema2;procedures
2;schema2;functions
EOF

It doesn't exexute Flymake, it just echo the Flymake commands.

It uses the special variable $IFS to do the magic.

Fit it to your needs.

output

Flyway -schema=schema1 -locations=/scripts/schema1/procedures, /scripts/schema1/functions, /scripts/schema1/packages migrate -x | tee /scripts/schema1/log/logfile_ddmmyy.log
Flyway -schema=schema1 -locations=/scripts/schema1/procedures, /scripts/schema1/functions, /scripts/schema1/packages migrate -x | tee /scripts/schema1/log/logfile_ddmmyy.log
Flyway -schema=schema2 -locations=/scripts/schema2/procedures, /scripts/schema2/functions, /scripts/schema2/packages migrate -x | tee /scripts/schema2/log/logfile_ddmmyy.log
Flyway -schema=schema2 -locations=/scripts/schema2/procedures, /scripts/schema2/functions, /scripts/schema2/packages migrate -x | tee /scripts/schema2/log/logfile_ddmmyy.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM