名前に特定の数字を含むテストファイル

名前に特定の数字を含むテストファイル

フォルダに$folderファイル2012-01,...,2012-N1,2013-01,...,2013-N2などが含まれているとしましょう。

私のスクリプトで自然数を要求し、M次のことをしたいと思います。

if the files 20M-* exist
then ask for other natural number M2 and
   if the file 20M-M2 exists
   then open it
else warning msg

ファイルが大幅に変更されるため、私のcaseテストはそれほど動的ではありません。

どんなアイデアがありますか?ありがとうございます。

ベストアンサー1

私はこれをよりエレガントにできると信じています。読者のための練習として残しておきます。 :血

echo M?
read M
for f in 20"$M"-*
do
    if [ -e "$f" ]
    then
        echo M2?
        read M2
        for f in 20"$M"-"$M2"
        do
            if [ -e "$f" ]
            then
                echo SUCCESS
                cat "$f"
                exit
            fi
        done
        echo FAIL: There is no file for 20"$M"-"$M2"
        exit 1
    fi
    echo FAIL: There is no file for 20"$M-*"
    exit 1
done

おすすめ記事