我在bash中有一个循环,将某些路径名设置为变量。
在该循环中,我想根据这些变量执行一些sqlite命令。
例如:
sqlitedb="/Users/Documents/database.db"
for mm in 01 02 03; do
filename1="A:/data1-${mm}.csv"
filename2="D:/data2-${mm}.csv"
sqlite3 "$sqlitedb" #create new file, it is a temporary file. no problem with this command.
.mode csv
.import "$filename1" data_1 #try to import the first data file. This command doesn't work
.import "$filename2" data_2 #try to import the second data file. This command doesn't work
# now do some important sql stuff which joins these files.
.quit
rm -f "$sqlitedb" #remove old file, ready for the next loop
done
很显然,SQLITE不了解我的BASH变量。在sqlite3中设置变量,循环访问文件等的最佳方法是什么?
如果有帮助,我正在使用WSL ubuntu 18.04
您需要一个heredoc,如注释中所述:
for mm in 01 02 03; do
filename1="A:/data1-${mm}.csv"
filename2="D:/data2-${mm}.csv"
sqlite3 -batch -csv <<EOF
.import "$filename1" data_1
.import "$filename2" data_2
-- Do stuff with the tables
EOF
done
((如果省略文件名,则sqlite使用内存数据库,因此除非需要存储大量数据,否则不需要手动的临时数据库文件)