unix 在shell脚本的for循环中调用提示符时,提示符无法正常工作

jgwigjjp  于 2023-05-06  发布在  Unix
关注(0)|答案(1)|浏览(233)

我正在修改一个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_connectionstexting.txtMax_user_connections=300diff.txtserver=000000server.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
yqkkidmi

yqkkidmi1#

进一步看了这个问题后,我想我理解你的要求了。这是我想出的一个解决方案。
假设Testing.txt包含:

Max_connections
Max_user_connections

diff.txt包含:

Max_connections=250
Max_user_connections=300

server.txt包含:

00000000
11111111

这段代码将遍历服务器,遍历要修改的变量(列在Testing.txt中),并从diff.txt中获取新值:

#!/bin/bash

while IFS= read -u 3 -r server
do
    printf '\nDEBUG: server=%s\n' "$server"
    while IFS= read -u 4 -r variable
    do
        printf '    DEBUG: var=%s\n' "$variable"
        newvalue=$(grep "$variable" diff.txt)
        read -e -r -p "    Do you want to comment >>$variable<< and add >>$newvalue<< for server >>$server<<? [yes|no] " reply
        printf '    DEBUG: reply=%s\n' "$reply"
        printf '    --> here put the modification to your target_my.cnf code\n\n'
    done 4< Testing.txt
done 3< server.txt

说明:

当我运行这段代码时,我得到以下输出:

./so.bash 

DEBUG: server=00000000
    DEBUG: var=Max_connections
    Do you want to comment >>Max_connections<< and add >>Max_connections=250<< for server >>00000000<<? [yes|no] yes
    DEBUG: reply=yes
    --> here put the modification to your target_my.cnf code

    DEBUG: var=Max_user_connections
    Do you want to comment >>Max_user_connections<< and add >>Max_user_connections=300<< for server >>00000000<<? [yes|no] no
    DEBUG: reply=no
    --> here put the modification to your target_my.cnf code

DEBUG: server=11111111
    DEBUG: var=Max_connections
    Do you want to comment >>Max_connections<< and add >>Max_connections=250<< for server >>11111111<<? [yes|no] yes
    DEBUG: reply=yes
    --> here put the modification to your target_my.cnf code

    DEBUG: var=Max_user_connections
    Do you want to comment >>Max_user_connections<< and add >>Max_user_connections=300<< for server >>11111111<<? [yes|no] no
    DEBUG: reply=no
    --> here put the modification to your target_my.cnf code

相关问题