我可以将错误与标准输出保存在同一文件中。我在下面使用了这种方法。问题是错误输出始终显示在顶部。在下面给出的示例中,该错误与倒数第二个sql命令有关,该命令无法保存值“ india”。错误消息应显示在该语句旁边,而不是文件的顶部。
# cat import.txt
drop table if exists testme;
create table testme (id int , name varchar(255));
insert into testme values (1, 'abc');
insert into testme values (2, 'abc', 'india');
insert into testme values (3, 'xyz');
# mysql test -vvf < import.txt >standard.txt 2>&1
# cat standard.txt
ERROR 1136 (21S01) at line 5: Column count doesn't match value count at row 1
--------------
drop table if exists testme
--------------
Query OK, 0 rows affected
--------------
create table testme (id int , name varchar(255))
--------------
Query OK, 0 rows affected
--------------
insert into testme values (1, 'abc')
--------------
Query OK, 1 row affected
--------------
insert into testme values (2, 'abc', 'india')
--------------
--------------
insert into testme values (3, 'xyz')
--------------
Query OK, 1 row affected
预期的输出将类似于以下内容...
# mysql test -vvf < import.txt
--------------
drop table if exists testme
--------------
Query OK, 0 rows affected
--------------
create table testme (id int , name varchar(255))
--------------
Query OK, 0 rows affected
--------------
insert into testme values (1, 'abc')
--------------
Query OK, 1 row affected
--------------
insert into testme values (2, 'abc', 'india')
--------------
ERROR 1136 (21S01) at line 4: Column count doesn't match value count at row 1
--------------
insert into testme values (3, 'xyz')
--------------
Query OK, 1 row affected
错误输出应恰好位于与其相关的语句的旁边。否则,重定向的输出文件将无用。
实际上,这与外壳无关,这是mysql
命令行实用程序的“功能” 。
基本上,当mysql检测到输出没有到达终端时,它将启用输出缓冲。这样可以提高性能。但是,程序显然会将成功输出发送到STDOUT,将错误输出发送到STDERR(这确实很有意义),并为每个驱动器保留一个单独的缓冲区。
解决的方法是简单地添加-n
到mysql命令参数。的-n
(或--unbuffered
)选项禁用输出缓冲。
例如:
mysql test -nvvf < import.txt >standard.txt 2>&1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句