Home » Questions » Computers [ Ask a new question ]

Traverse path up and check/set permissions in shell script - how?

Traverse path up and check/set permissions in shell script - how?

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")?

Asked by: Guest | Views: 319
Total answers/comments: 2
Guest [Entry]

"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"
Guest [Entry]

"To expand on ennuikiller's solution:

realpath () {
# realpath() -- print absolute path (os.path.realpath) to $1
# note: OSX does not have readlink -f
python -c ""import os,sys; print(os.path.realpath(os.path.expanduser(sys.argv[1])))"" ""${1}""
return
}

walkpath () {
# walkpath() -- walk down path $1 and $cmd each component
# $1 : path (optional; default: pwd)
# $2 : cmd (optional; default: ls -ald --color=auto)
#superuser.com/a/65076
dir=${1:-$(pwd)}
if [ $(uname) == ""Darwin"" ]; then
cmd=${2:-""ls -ldaG""}
else
cmd=${2:-""ls -lda --color=auto""}
fi
dir=$(realpath ${dir})
parts=$(echo ${dir} \
| awk 'BEGIN{FS=""/""}{for (i=1; i < NF+2; i++) print $i}')
paths=('/')
unset path
for part in $parts; do
path=""$path/$part""
paths=(""${paths[@]}"" $path)
done
${cmd} ${paths[@]}
}"