フォルダを選択するBashスクリプト

フォルダを選択するBashスクリプト

最近変更されたすべてのフォルダを一覧表示したディレクトリを最初に使用しようとしましたが、動作が停止しselectました。

私が持っているとしましょう:

Folder1
ThisIsAnotherDir
Directory
New Directory
This IS not_Same Directory

次のコマンドを実行するとき:

ls -t -d */ 

希望の出力を取得します。

しかし、私が使用するときselect

options=$(ls -t -d */)
select d in $options; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done

最初に変更されたフォルダが一覧表示されますが、New Directory最後に変更して実行すると、次のように出力されます。

1)New 
2)Directory
3)Folder1
4)ThisIsAnotherDir
5)Directory
6)This 
7)IS 
8)not_Same 
9)Directory

私も次のことを試しました。

select d in "$options[0]"; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done

また失敗しました。

これが意味があることを願っています。ありがとう

ベストアンサー1

回答の組み合わせhttps://unix.stackexchange.com/a/378304/330217ls -tあなたのコマンドで試してみることができます

IFS= mapfile -t files < <(ls -t -d */)

select d in "${files[@]}"
do
    test -n "$d" && break
    echo ">>> Wrong Folder Selection!!! Try Again"
done

ディレクトリ名に改行文字(またはその他の特殊文字)が含まれている場合、この回避策は機能しません。

おすすめ記事