许多PostgreSQL文件的Echo输出

时间:2016-08-19 07:26:02

标签: linux bash postgresql shell command-line

我在Fedora 24上使用PostgreSQL 9.5。

我有以下文件:

 $ cat hello.sql
SELECT 'hello';
 $ cat world.sql
SELECT 'world';

下面有两个执行:

 $ psql samples -f hello.sql -f world.sql
 ?column? 
----------
 world
(1 row)

 $ psql samples -f world.sql -f hello.sql
 ?column? 
----------
 hello
(1 row)

如何查看两个脚本的输出?

3 个答案:

答案 0 :(得分:3)

或者你可以:

$ cat hello.sql world.sql |psql -f -
 ?column?
----------
 hello
(1 row)

 ?column?
----------
 world
(1 row)

答案 1 :(得分:0)

我理解的错误是尝试在单个命令中执行许多PostgreSQL文件。我应该使用以下bash脚本:

 $ cat hello_world.sh
SQL_FILES=(hello.sql world.sql)
for SQL_FILE in ${SQL_FILES[@]}
do
    psql samples -f $SQL_FILE
done

以下执行正是我的需要:

 $ ./hello_world.sh
 ?column? 
----------
 hello
(1 row)

 ?column? 
----------
 world
(1 row)

答案 2 :(得分:0)

你可以使用如下;

for sqlfile in hello.sql world.sql; do psql samples -f "$sqlfile"; done