linux - How to identify directory paths within multi-line string, replace with references to own file content, in Bash? -
given bash variable containing following multi-line string representing *nix paths:
/some/app/path/dir1 /some/app/path/somefile1.txt /some/other/app/path/somefile2.txt /some/random/app/path/dir2 i'd able identify directories (dir1 & dir2, example) , replace these directory path lines paths of files below them. there should no directory references in final output, thus:
#nodirectory references, files. /some/app/path/dir1/dir3/file1.xml /some/app/path/dir1/dir3/file2.doc /some/app/path/somefile1.txt /some/other/app/path/somefile2.txt /some/random/app/path/dir2/file3.png /some/random/app/path/dir2/dir4/file4.txt i'm not sure how iterate on lines , remove/replace directories, how this?
i'm assuming use of 'if' , various operators identify directories, , 'find /my/dir -type f', find files.
i'm using mac osx 10.10 (yosemite).
test if name directory. if is, call find, otherwise echo is.
echo "$variable" | while ifs= read -r name if [[ -d $name ]] find "$name" -type f -print else echo "$name" fi done instead of piping echo "$variable", can use here-string:
while ifs= read -r name if [[ -d $name ]] find "$name" -type f -print else echo "$name" fi done <<<"$variable"
Comments
Post a Comment