使用shell脚本从sqlite导出到csv [英] Export from sqlite to csv using shell script
问题描述
我使用shell脚本将sqlite查询导出到csv文件,如下所示:
#! / bin / bash
./bin/sqlite3 ./sys/xserve_sqlite.db.headers上的
./bin/sqlite3 ./sys/xserve_sqlite.db.mode csv
./bin/sqlite3 ./sys/xserve_sqlite.db.output out.csv
./bin/sqlite3 ./sys/xserve_sqlite.dbselect * from eS1100_sensor_results;
./bin/sqlite3 ./sys/xserve_sqlite.db.exit
执行脚本,输出显示在屏幕上,而不是保存到out.csv。它正在使用与命令行相同的方法,但我不知道为什么shell脚本无法导出数据到该文件。
我做错了什么?
sqlite3
$ c> sqlite3 ;在您的选择
运行时,您的 .out out.csv
已被遗忘。
尝试:
#!/ bin / bash
./bin/sqlite3。 /sys/xserve_sqlite.db<< ;!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!
sh / bash方法
您可以使用重新导向呼叫您的指令码:
$ your_script> out.csv
或者您可以在脚本中插入以下内容作为第一行: / p>
exec> out.csv
$ b b
前面的方法允许你指定不同的文件名,而后者输出到一个特定的文件名。在这两种情况下, .output out.csv
行都可以忽略。
I'm making a shell script to export a sqlite query to a csv file, just like this:
#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db ".headers on"
./bin/sqlite3 ./sys/xserve_sqlite.db ".mode csv"
./bin/sqlite3 ./sys/xserve_sqlite.db ".output out.csv"
./bin/sqlite3 ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;"
./bin/sqlite3 ./sys/xserve_sqlite.db ".exit"
When executing the script, the output apears on the screen, instead of being saved to "out.csv". It's working doing the same method with the command line, but I don't know why the shell script fails to export data to the file.
What am I doing wrong?
解决方案 sqlite3
You have a separate call to sqlite3
for each line; by the time your select
runs, your .out out.csv
has been forgotten.
Try:
#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db <<!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!
instead.
sh/bash methods
You can either call your script with a redirection:
$ your_script >out.csv
or you can insert the following as a first line in your script:
exec >out.csv
The former method allows you to specify different filenames, while the latter outputs to a specific filename. In both cases the line .output out.csv
can be ignored.
这篇关于使用shell脚本从sqlite导出到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!