简体   繁体   English

Sed 从文件中删除日志错误实例,但前提是文件不存在?

[英]Sed removing instances of log errors from a file, but only if the file doesn't exist?

Please note: this is purely a bash scripting and sed question, even though it mentions a few other technologies (MySQL, Docker, etc.) in passing, for context.请注意:这纯粹是一个 bash 脚本和sed问题,尽管它提到了一些其他技术(MySQL、Docker 等)。


Looking at a bash script that runs when the official MySQL 8.0 Dockerfile builds, I see this:查看在官方MySQL 8.0 Dockerfile构建时运行的bash 脚本,我看到了:

if [ -n "$MYSQL_LOG_CONSOLE" ] || [ -n "console" ]; then
  # Don't touch bind-mounted config files
  if ! cat /proc/1/mounts | grep "etc/my.cnf"; then
    sed -i 's/^log-error=/#&/' /etc/my.cnf
  fi
fi

...and I'm trying to make sense of what it does. ......我试图理解它的作用。 A few things don't make sense to me, but this is from the official MySQL Docker project, so obviously they know what they're doing.有些事情对我来说没有意义,但这是来自官方 MySQL Docker 项目,所以显然他们知道他们在做什么。 It looks like its saying:看起来像它的说法:

  1. check to see if a string variable called MYSQL_LOG_CONSOLE is not null, or if the hard-coded string literal "console" is not null检查名为MYSQL_LOG_CONSOLE的字符串变量是否不是 null,或者硬编码字符串文字“console”是否不是 null
  2. If either are true, and then search available mount info ( proc/1/mounts ) to see if etc/my.cnf is referenced in it如果其中一个为真,则搜索可用的挂载信息( proc/1/mounts )以查看其中是否引用了etc/my.cnf
  3. If etc/my.cnf is not contained inside of proc/1/mounts , then remove all instances of log-error from inside etc/my.cnf如果etc/my.cnf不包含在proc/1/mounts中,则从etc/my.cnf中删除所有log-error实例

To start with, what's the point of checking whether the string literal "console" is null or not with [ -n "console" ] ?首先,使用[ -n "console" ]检查字符串文字 "console" 是否为 null 有什么意义? How could a non-null string literal ever be...null?非空字符串文字怎么可能是...null?

But the main question I have though is with the sed :但我的主要问题是sed

sed -i 's/^log-error=/#&/' /etc/my.cnf

Am I interpreting this sed command correctly?我是否正确解释了这个sed命令? Remove all instance of log-error from the file...if the file doesn't exist?, I'm clearly missing something.从文件中删除所有log-error实例......如果文件不存在?,我显然错过了一些东西。 just not sure what.只是不确定是什么。 Thanks in advance!提前致谢!

what's the point of checking whether the string literal "console" is null or not with [ -n "console" ]?使用 [ -n "console" ] 检查字符串文字“console”是否为 null 有什么意义?

The file is generated from the template :该文件是从模板生成的:

 if [ -n "$MYSQL_LOG_CONSOLE" ] || [ -n "%%DEFAULT_LOG%%" ]; then

Logging to console is by default enabled only in mysql 8, from generating script :默认情况下,登录到控制台仅在 mysql 8 中启用,从生成脚本

 declare -A DEFAULT_LOG DEFAULT_LOG["5.6"]="" DEFAULT_LOG["5.7"]="" DEFAULT_LOG["8.0"]="console"

How could a non-null string literal ever be...null?非空字符串文字怎么可能是...null?

It cannot be.它不可能是。

Am I interpreting this sed command correctly?我是否正确解释了这个 sed 命令?

No. The sed command doesn't remove the lines, it prepends # in front of the lines starting with log-error= .不。 sed命令不会删除这些行,它会在以log-error=开头的行前面添加#

Remove all instance of log-error from the file...从文件中删除所有日志错误实例...

No.不。

if the file doesn't exist?!如果文件不存在?!

No, only if the file is not mounted.不,仅当文件未挂载时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM