Bash- Extract every smaller path out of a full path -
i'm trying take full path directory , extract every path out of it. example
fullpath="/home/me/something/file" /home/me/something/file /home/me/something /home/me /home
my attempt haven't been able figure out how bring paths out.
ifs=$'/' in $restorepath echo $i if [ -d $i ]; echo dir exists fi done unset ifs
couple of issues:
- you're testing directory, looks you're providing file
- there no reason
unset ifs
since change won't propagated parent shell. if want re-set default 1 use later in script, can save old 1 in variable , reset after, or change commonifs=$'\t\n '
try this
#!/bin/bash fullpath="/home/me/something/file" ifs='/' in $fullpath; subpath+="$i/" if [[ -e $subpath ]]; echo "$subpath" elif [[ -e ${subpath%?} ]]; echo "${subpath%?}" && break else break fi done
note, ${subpath%?}
chops off last character (?
= single character wildcard in bash), in case entered file , not directory input. it's safe break loop if subpath doesn't exist, since implies no descendant of can exist.
Comments
Post a Comment