简体   繁体   English

用户提示后重击+打印默认字符

[英]Bash + print default character after user prompt

-I made a bash function like this: -我做了一个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
}

Now I want to expand the function for the enduser like this : 现在,我要扩大这样的终端用户功能:

./finditems.sh

Trying to find stuff and then do some stuff after confirmation. 尝试查找内容,然后在确认后做一些事情。

Found item: /dir/mod.item

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

How can I print the "user removable" y character on the user's shell prompt? 如何在用户的shell提示符上打印“用户可移动” y字符?

I tried some variations with read -p , but it didn't give me the result that I want. 我用read -p尝试了一些变体,但没有得到想要的结果。

对于bash 4+(我相信),如果您将readline与read一起使用,则可以使用-i text参数将read读取为预种子输入。

This implementation seems to complex. 这种实现似乎很复杂。 I have found another solution: By adding double quotes to y|\\Y|"") I have eliminated the need for a 'y' character and questionable or unnecessary code. 我找到了另一种解决方案:通过在y | \\ Y |“”)中添加双引号,消除了对'y'字符和可疑或不必要代码的需求。 By pressing 'return' after the "Is this ok? y/n [y]:" statement the for-loop continues as expected. 通过在“这行吗?y / n [y]:”语句之后按“ return”键,for循环将按预期继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM