批处理文件读取文件内容并迭代
batch file to read file contents and iterate
我正在尝试编写一个批处理 file/shell 脚本来读取文件的内容,将这些内容与目录结构相匹配并调用一个可执行文件。
假设 /system/ 目录中有一个 sequence.txt 文件。序列文件是为了表示或强制执行的顺序。这很重要
sequence.txt 文件有以下条目:
1;schema1;procedures
1;schema1;functions
2;schema2;procedures
2;schema2;functions
........
然后是一个目录,目录下有下面的子目录
/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
.......
........
现在我想运行 flyway(一个数据库迁移软件)循环使用这种方式:
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
到目前为止,这是我的进度:
#!/bin/bash
while read i;
do echo "$i";
done < ./system/sequence.txt
我该如何继续?我知道这种脚本涉及到变量,然后循环,但我找不到将其转化为技术层面的方法。
提前干杯和感谢! :-)
我不确定值和最终命令路径之间的确切关系,但 awk 是构建调用的工具。使用类似:
c = `echo $i | awk -F ";" '{print "flyway" "_" }'
其中 $x 是值的位置,您可以构造一个字符串。
之后你可以运行 c var with
`echo $c`
应该可以。
更新:
如果我理解正确你需要什么,你必须设置两个while,一个在另一个里面。像这样:
cat tt.txt | awk -F";" '{print }'| sort -u | while read i
do
sc = `grep $i tt.txt | head -n 1 | awk -F";" '{print }'`
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 }'| sort -u`
pt2 = $pt2 "/scripts/" $sc "/" $c ""
done
echo $pt1 $pt2 $pt3
done
虽然不是很清楚你想要什么,但这里有一些启发:
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
它不执行 Flymake,它只是 echo
Flymake 命令。
它使用特殊变量 $IFS
来施展魔法。
满足您的需求。
输出
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
我正在尝试编写一个批处理 file/shell 脚本来读取文件的内容,将这些内容与目录结构相匹配并调用一个可执行文件。
假设 /system/ 目录中有一个 sequence.txt 文件。序列文件是为了表示或强制执行的顺序。这很重要
sequence.txt 文件有以下条目:
1;schema1;procedures
1;schema1;functions
2;schema2;procedures
2;schema2;functions
........
然后是一个目录,目录下有下面的子目录
/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
.......
........
现在我想运行 flyway(一个数据库迁移软件)循环使用这种方式:
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
到目前为止,这是我的进度:
#!/bin/bash
while read i;
do echo "$i";
done < ./system/sequence.txt
我该如何继续?我知道这种脚本涉及到变量,然后循环,但我找不到将其转化为技术层面的方法。
提前干杯和感谢! :-)
我不确定值和最终命令路径之间的确切关系,但 awk 是构建调用的工具。使用类似:
c = `echo $i | awk -F ";" '{print "flyway" "_" }'
其中 $x 是值的位置,您可以构造一个字符串。
之后你可以运行 c var with
`echo $c`
应该可以。
更新:
如果我理解正确你需要什么,你必须设置两个while,一个在另一个里面。像这样:
cat tt.txt | awk -F";" '{print }'| sort -u | while read i
do
sc = `grep $i tt.txt | head -n 1 | awk -F";" '{print }'`
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 }'| sort -u`
pt2 = $pt2 "/scripts/" $sc "/" $c ""
done
echo $pt1 $pt2 $pt3
done
虽然不是很清楚你想要什么,但这里有一些启发:
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
它不执行 Flymake,它只是 echo
Flymake 命令。
它使用特殊变量 $IFS
来施展魔法。
满足您的需求。
输出
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