当我尝试查看 mysql 的历史记录时 - 我看到了这个
cat ~/.mysql_history
我明白了:
└── cat ~/.mysql_history
_HiStOrY_V2_
exit
GRANT\040ALL\040PRIVILEGES\040ON\040*\040.\040*\040TO\040'jada'@'localhost';
FLUSH\040PRIVILEGES;
select\040User,Host\040from\040mysql.user;
exit
select\040User,Host\040from\040mysql.user;
GRANT\040ALL\040PRIVILEGES\040ON\040*\040.\040*\040TO\040'jada'@'localhost';
FLUSH\040PRIVILEGES;
select\040User,Host\040from\040mysql.user;
exit
drop\040database\app;\040
show\040databases;\040
create\040database\app;\040
exit
show\040databases;\040
use\app;\040
select\040*\040from\040users;\040
exit
use\app;\040
select\040*\040from\040users;\040
exit
select\040User,Host\040from\040mysql.user;
GRANT\040ALL\040PRIVILEGES\040ON\040*\040.\040*\040TO\040'jada'@'localhost';
FLUSH\040PRIVILEGES;
select\040User,Host\040from\040mysql.user;
exit
select\040User,Host\040from\040mysql.user;
exit
select\040*\040from\040users;
show\040databases;\040
use\app\040;\040
select\040*\040from\040users;
DELETE\040FROM\040table_name\040WHERE\040condition;
DELETE\040FROM\040users\040WHERE\040id\040=\0402;
select\040*\040from\040users;
history
show\040history;
exit
我看到了一堆
\040
。
如何在 bash 中看到像常规历史记录一样的干净历史记录?
我已经在 5 个虚拟机上尝试过。相同的结果。一直以来。
我记得mysql客户端上有一个老bug https://bugs.mysql.com/bug.php?id=68925 尝试更新一下
sed 's/\\040/ /g' < .mysql_history
这仅修复空格;我不知道问题的严重程度。
\0xx
是八进制转义序列。 40
是 space
的八进制。
perl -pe 's/\\([0-7]{1,3})/chr oct $1/eg' <.mysql_history
这将解决八进制表示(不仅仅是空格)。
演示/测试:
perl -pe 's/\\([0-7]{1,3})/chr oct $1/eg' <<<"SELECT\040\042String\042;"
SELECT "String";
逐行,大多数情况下如果需要一行,你可以
echo -e "GRANT\040ALL\040PRIVILEGES\040ON\040*\040.\040*\040TO\040'jada'@'localhost';"
GRANT ALL PRIVILEGES ON * . * TO 'jada'@'localhost';
printf %b\\n "GRANT\040ALL\040PRIVILEGES\040ON\040*\040.\040*\040TO\040'jada'@'localhost';"
GRANT ALL PRIVILEGES ON * . * TO 'jada'@'localhost';
当然,您可以循环整个文件:
while read -r line ;do
printf '%b\n' "$line"
done <.mysql_history
sed
彻底
你必须运行
sed
两次:
sed "$(sed 's/\\\([0-9]\{3\}\)/\\o\1/g;s/^/1a\\/')" <<<''
对于标准输入或
sed "$(sed 's/\\\([0-9]\{3\}\)/\\o\1/g;s/^/1a\\/' .mysql_history)" <<<''
您可以简单地使用 sed 命令替换空格字符并为其定义一个别名,这样您就可以简单地调用 alias 而不是重复完整的命令:Alias mysqlh for mysql History
$ alias mysqlh="cat ~/.mysql_history | sed 's/\\\040/ /g'"
$ mysqlh
_HiStOrY_V2_
exit
GRANT ALL PRIVILEGES ON * . * TO 'jada'@'localhost';
FLUSH PRIVILEGES;
select User,Host from mysql.user;
exit
select User,Host from mysql.user;
GRANT ALL PRIVILEGES ON * . * TO 'jada'@'localhost';
FLUSH PRIVILEGES;
select User,Host from mysql.user;
exit
drop database\app;
show databases;
create database\app;
exit
show databases;
use\app;
select * from users;
exit
use\app;
select * from users;
exit
select User,Host from mysql.user;
GRANT ALL PRIVILEGES ON * . * TO 'jada'@'localhost';
FLUSH PRIVILEGES;
select User,Host from mysql.user;
exit
select User,Host from mysql.user;
exit
select * from users;
show databases;
use\app ;
select * from users;
DELETE FROM table_name WHERE condition;
DELETE FROM users WHERE id = 2;
select * from users;
history
show history;
exit