In a given shell script I need to take a known path and check it upwards (to the file system root) for correct permissions. How would I split the path and walk upwards in a shell script (can be bash or a "lower common denominator")?

link|improve this question
feedback

2 Answers

up vote 0 down vote accepted

This should work:

unset path
parts=$(pwd | awk 'BEGIN{FS="/"}{for (i=1; i < NF; i++) print $i}')

for part in $parts
do
path="$path/$part"
ls -ld $path   # do whatever checking you need here
done
link|improve this answer
Thanks, that works. Is there a (ba)sh-only solution? – k-fish Nov 5 '09 at 21:12
feedback

When I tried ennuikiller's awk solution, it missed the current directory for me.

So, while a bit late, here's a bash-only solution:

foo=`pwd`
while [ "$foo" != "" ]; do
    echo $foo # work with directory here
    foo=${foo%/*}
done

Don't know if it's the most elegant solution, but it works. I found a simple explanation of the string manipulation on tldp and wrapped a loop around it.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.