Bash + 在用户提示后打印默认字符

Bash + print default character after user prompt

-我做了一个这样的bash函数:

find_stuff () {
list=`find $SEARCHPATH -type f -user $USERID -mmin -$SOMETIME -not -path '*/\.*' -exec ls -1rt {} \+`
     if [ -n "$list" ]; then
        for i in $list; do
            echo "Found item: "$i""; printf 'Is this ok? y/n [y]:'
            read arg
            case "arg" in
                y|\Y)
                    do_stuff
                    ;;
                n|\N)
                    continue
                    ;;
                *)
                    echo "Please type 'y'=yes or 'n'=no, -exiting..."
                    exit 0
                    ;;
            esac
        done
     else
         echo "No items found, exiting..."
         exit 0
     fi
}

现在我想为最终用户扩展功能,例如 this:

./finditems.sh

正在尝试寻找东西,然后在确认后做一些事情。

Found item: /dir/mod.item

Is this ok? y/n [y]:y

如何在用户的 shell 提示中打印 "user removable" y 字符?

我尝试了 read -p 的一些变化,但没有给我想要的结果。

With bash 4+(我相信)如果您将 readline 与 read 一起使用,那么您可以使用 -i text 参数来读取预播种输入。

这个实现似乎很复杂。我找到了另一个解决方案: 通过向 y|\Y|"") 添加双引号,我消除了对 'y' 字符和可疑或不必要代码的需要。 通过在 "Is this ok? y/n [y]:" 语句之后按 'return',for 循环将按预期继续。