io-redirection – 将错误重定向到标准输出
作者:互联网
我可以将错误保存在与标准输出相同的文件中.我在下面使用过这种方法.
问题是错误输出“始终”显示在顶部.在下面给出的示例中,错误与第二个最后一个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
错误输出应紧跟在与其相关的语句旁边.否则重定向的输出文件没用.
解决方法:
这实际上与shell无关,它是mysql命令行实用程序的“功能”.
基本上当mysql检测到输出没有到达终端时,它会启用输出缓冲.这提高了性能.然而,该程序显然将成功输出发送到STDOUT,并将错误输出发送到STDERR(确实有意义),并为每个输出保留一个单独的缓冲区.
解决方案只是将-n添加到mysql命令参数中. -n(或–unbuffered)选项禁用输出缓冲.
例如:
mysql test -nvvf < import.txt >standard.txt 2>&1
标签:mysql,io-redirection 来源: https://codeday.me/bug/20190814/1651554.html