linux - 捕获错误并要求在 BASH 中手动输入

标签 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]" ] ; 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

有没有办法让代码选择在失败时请求手动位置?

最佳答案

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

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

关于linux - 捕获错误并要求在 BASH 中手动输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26552872/

相关文章:

linux - Bash - 找到每行的最小数量

bash - 在命令行上使用粘贴或 PR 加入列不起作用

python - 通过 bash 文件运行 anaconda

linux - 在 awk 命令中使用 bash 脚本 $1 参数

Linux 用户空间程序显示其内部计数器,类似于/proc/meminfo

c - linux中如何获取指定线程的注册信息?

linux - 适用于 Linux 的交互式 GUI 命令行合并工具(如 BeyondCompare 或 WinMerge)

linux非标准串行控制台

linux - 从文件中获取文件 glob 并找到这些文件

linux - 如何将文本文件中杂乱的内容排序成单列