パスを順次分割[閉じる]

パスを順次分割[閉じる]

このように道を分かなければなりません。

/sandy/user1/user2/abc.txt
/sandy/user1/user2
/sandy/user1
/sandy
/

ベストアンサー1

あなたが何を求めているのか完全にはわかりませんが、ここに暗いシーンがあります。

path=/sandy/user1/user2/abc.txt
while [ "$path" ]; do
    printf '%s ' "$path"
    path=${path%/*} # remove trailing path component
done
echo /

出力:/sandy/user1/user2/abc.txt /sandy/user1/user2 /sandy/user1 /sandy /

おすすめ記事