16

我正在尝试将目录中的文件用作 bash 脚本中的选项。用户应该能够选择一个,然后将所选文件的名称传递给变量。到目前为止,我可以获得文件列表,但经过几个小时的尝试,我无法弄清楚如何将它们显示为选项。

#!/bin/bash
prompt="Please select a file:"
options=( $(find -maxdepth 1 -print0 | xargs -0) )

PS3="$prompt "
select opt in "${options[@]}" "Quit"; do 

    case "$REPLY" in
    for i in "${options[@]}"
    do
    $i' ) echo "You picked $opt which is file $REPLY";;'
    done    
    $(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
    *) echo "Invalid option. Try another one.";continue;;

    esac

done

任何帮助深表感谢。谢谢!

4

2 回答 2

24

case我认为这里不适合:

#!/bin/bash
prompt="Please select a file:"
options=( $(find -maxdepth 1 -print0 | xargs -0) )

PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do 
    if (( REPLY == 1 + ${#options[@]} )) ; then
        exit

    elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
        echo  "You picked $opt which is file $REPLY"
        break

    else
        echo "Invalid option. Try another one."
    fi
done    

ls -ld $opt
于 2013-04-04T09:58:23.823 回答
0

不要错过 percol、fzy、smenu 和朋友们。他们会很高兴地从标准输入中获取,呈现一个带有交互式过滤器的用户友好的选择菜单,然后再次将选定的行输出。

于 2017-11-08T19:37:21.030 回答