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 に答える