Linux 捕获错误并在BASH中请求手动输入

Linux 捕获错误并在BASH中请求手动输入,linux,bash,shell,Linux,Bash,Shell,我有以下代码: if [ -e /etc/my.cnf ]; then _out_fn="/etc/my.cnf" if [ ! -e ${_out_fn}_orig ] ; then cp $_out_fn ${_out_fn}_orig fi cp /dev/null $_out_fn while read line ; do set -- $line if [ "$1" = "[mysqld]"

我有以下代码:

if [ -e /etc/my.cnf ]; then
    _out_fn="/etc/my.cnf"
        if [ ! -e ${_out_fn}_orig ] ; then
        cp $_out_fn ${_out_fn}_orig
        fi
    cp /dev/null $_out_fn
    while read line ; do
    set -- $line
        if [ "$1" = "[mysqld]" ] ; then
            echo $line >> $_out_fn
            line="safe-show-database
            skip-networking
            local-infile=0
            symbolic-links=0"
        fi
    echo $line >> $_out_fn
    done < ${_out_fn}_orig
    echo "$MySQL Securing Complete! "
else
    echo "$MySQL Configuration File Not Found! "
fi
if[-e/etc/my.cnf];然后
_out_fn=“/etc/my.cnf”
如果[!-e${u out\u fn}{u orig];然后
cp$\u out\u fn${u out\u fn}\u orig
fi
cp/dev/null$\u out\u fn
读行时;做
设置--$line
如果[“$1”=“[mysqld]”;然后
echo$line>>$\u out\u fn
line=“安全显示数据库
跳过网络
局部填充=0
符号链接=0“
fi
echo$line>>$\u out\u fn
完成<${u out\u fn}
echo“$MySQL安全保护完成!”
其他的
echo“$MySQL配置文件未找到!”
fi

有没有办法让代码在出现故障时选择手动定位

使用
读取
。要求手动输入的代码段可能如下所示:

config_location=
while : ; do
        echo -n "Enter the path to the configuration file: "
        read config_location
        if test -r "$config_location"; then
                break
        fi
        echo "File $config_location not found or is unreadable, let's try again."
done
echo "OK, $config_location exists."
在这里,我们继续询问用户,直到他提供的路径存在并且可读(
test-r
)。
while:;do
是一个无止境的循环。如果只需要询问一次路径,则只需要以下内容:

echo -n "Enter the path to the configuration file: "
read config_location

最有可能的情况是,如果发生什么故障,代码中已经有一个else子句。。如果my.cnf位于其他位置或
while,请询问输入位置-r“$config_location”;执行…