我正在修改一个mysql配置文件,基于两个文件(源和目标mysql配置文件)之间的差异,这将在diff.txt
文件中生成,我将搜索testing.txt
中存在的值,在添加diff.txt
中存在的值之前,在目标mysql配置文件中注解它们。
在代码中,我在for
循环中给出了提示,以便在继续添加值之前打印确认,但提示无法正常工作,它仅在以下文件中有单个值时才有效。
diff command used:diff target_my.cnf source_my.cnf | grep '>' | sed 's/> *//' > diff.txt
diff.txt
包含以下内容:
Max_connections=250
Max_user_connections=300
server.txt
具有服务器名称
00000000
testing.txt
具有以下功能
Max_connections
Max_user_connections
需要实现:
1.我需要提示显示,就像它应该从testing.txt
中选择Max_connections
,从diff.txt
中选择max_connections=250
,从server.txt
中选择server=00000000
,并要求下面的预期提示。
1.同样的事情,第二个值Max_user_connections
从texting.txt
和Max_user_connections=300
从diff.txt
和server=000000
从server.txt
,并要求下面的提示,并在那里结束它自己。但在我的情况下,我没有得到适当的提示。
预期的提示语句:
Do you want to comment env(max_user_connections) and add (max_user_connections=300) in server(0000000) after verifying? [yes/no]
Do you want to comment env(max_connections) and add (max_connections=250)in server(0000000) after verifying? [yes/no]
但根据我的代码,它以分散的方式显示如下提示:
Do you want to comment env(max_user_connections) and add (max_user_connections=300) in server(0000000) after verifying? [yes/no]
Do you want to comment env(max_connections) and add (max_user_connections=300) in server(0000000) after verifying? [yes/no]
Do you want to comment env(max_user_connections) and add (max_connections=250) in server(0000000) after verifying? [yes/no]
Do you want to comment env(max_connections) and add (max_connections=250) in server(0000000) after verifying? [yes/no]
需要帮助获得正确的提示,以便我可以修改配置文件。
所用代码:
function ask{
question="$1"
ok=0
while [ $ok -eq 0 ]; do
read -p " $question [yes/no] " reply
if [ "$reply" == "yes" ] || [ "$reply" == "no" ]; then
ok=1
done
echo -n "$reply"
}
for server in `cat server.txt`
do
for var in `cat diff.txt`
do
var1=$(sed "/=[^=]*$//g" diff.txt)
echo "$var1" > testing.txt
for var2 in `cat testing.txt`
reply=$(ask "do you want to comment($var2) and add($var) for server($server?")
if [ "$reply" == "yes" ]; then
sed -i "s/^${var1}/#&/" target_my.cnf
sed -i "/^#${var1}/a ${var}" target_my.cnf
else
echo -e "Skipping"
fi
done
done
done
尝试的其他方法:
for server in `cat server.txt`
do
for var in `cat diff.txt`
do
var1=$(sed "/=[^=]*$//g" diff.txt)
echo "$var1" > testing.txt
for var2 in `cat testing.txt`
echo "do you want to comment($var2) and add($var) for server($server?")
read -r reply
if [ "$reply" == "yes" ]; then
sed -i "s/^${var1}/#&/" target_my.cnf
sed -i "/^#${var1}/a ${var}" target_my.cnf
else
echo -e "Skipping"
fi
done
done
done
尝试使用while循环,但出现相同的提示:
while read -r server ; do
while read -r var ; do
While read -r var2 ; do
echo -e "do you want to comment${var2} and add${var} for server${server}?"
read -r reply < /dev/tty
if [ "$reply" == "yes" ]; then
sed -i "s/^${var1}/#&/" target_my.cnf
sed -i "/^#${var1}/a ${var}" target_my.cnf
else
echo -e "Skipping"
fi
done < documented.txt
done < testing.txt
done < server_my.cnf.txt
1条答案
按热度按时间yqkkidmi1#
进一步看了这个问题后,我想我理解你的要求了。这是我想出的一个解决方案。
假设Testing.txt包含:
diff.txt包含:
server.txt包含:
这段代码将遍历服务器,遍历要修改的变量(列在Testing.txt中),并从diff.txt中获取新值:
说明:
while
方法来自https://mywiki.wooledge.org/BashFAQ/001。read -u 3 -r server
和read -u 4 -r variable
。这是从BASH solution to embedded read -p within a while read loop。每次读取都必须使用自己的文件处理程序,否则第二个while
中的read
将从文件中读取,而不是从终端读取。read -e
:从终端读取一行。read -r
:与反斜杠相关,读作https://ss64.com/bash/read.html。read -p
:不需要单独的echo
或printf
,read
可以为您完成此操作。newvalue
可以变得更复杂,如果你只需要值,而不是整行。当我运行这段代码时,我得到以下输出: