gpt4 book ai didi

bash - 有谁知道仅使用 BaSH 在没有 vim、emacs 或 nano 的情况下编辑文本文件的绝妙技巧?

转载 作者:行者123 更新时间:2023-12-02 19:14:19 25 4
gpt4 key购买 nike

对于上下文,我正在尝试为 MySQL DB 创建一个 docker 镜像,我可以在不使用密码的情况下登录。我正在阅读 article关于如何做到这一点,但它需要我使用我需要的凭据更新容器中的 my.cnf 以避免需要密码。
我的一个想法是获取 my.cnf 文件的样板版本,将其放入普通文本文件中,使用我需要的凭据对其进行更新,然后将其复制到容器中。但是我仍然不知道足够的 BASH 命令来从一个文件中获取文本并覆盖另一个文件。我什至只接受关于如何使用 vanilla shell 命令来完成它的指针。希望这可以帮助其他人发布此问题。
这是 my.cnf 本地人最初喜欢的内容

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
我需要更新它以获得这样的东西。此代码段包含用户和密码行项目。
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[MySQL]
user=root
password=supersecret

最佳答案

由于您想要进行的更改只是添加到文件的末尾,因此您可以使用带有 >> 的输出重定向。附加到文件中。以下任何一项都应该有效。下面是一个使用 shell “here-document”的例子:

cat >>.my.cnf <<'EOF'
[MySQL]
user=root
password=supersecret
EOF
或者您可以使用三个单独的 echo年代:
echo '[MySQL]' >>.my.cnf
echo 'user=root' >>.my.cnf
echo 'password=supersecret' >>.my.cnf
或单个 printf :
printf '%s\n' '[MySQL]' 'user=root' 'password=supersecret' >>.my.cnf
请注意,如果您的密码包含单引号,则需要在 echo 中进行更复杂的处理。或 printf案例(因为您不能在单引号字符串中包含单引号)。

关于bash - 有谁知道仅使用 BaSH 在没有 vim、emacs 或 nano 的情况下编辑文本文件的绝妙技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64343982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com