[英]How to identify directory paths within multi-line string, replace with references to own file content, in Bash?
給定一個bash變量,其中包含以下表示* nix路徑的多行字符串:
/some/app/path/dir1
/some/app/path/somefile1.txt
/some/other/app/path/somefile2.txt
/some/random/app/path/dir2
我希望能夠識別所有目錄(例如,dir1和dir2), 並將這些目錄路徑行替換為它們下面文件的路徑。 最終輸出中應該沒有目錄引用,因此:
#Nodirectory references, just 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
我不確定確切如何遍歷行並刪除/替換目錄,我該怎么做?
我假設使用'if'和各種運算符來標識目錄,並使用'find / my / dir -type f'來實際查找文件。
我正在使用Mac OSX 10.10(Yosemite)。
測試名稱是否為目錄。 如果是這樣,請調用find
,否則只需按原樣回顯即可。
echo "$variable" |
while IFS= read -r name
do
if [[ -d $name ]]
then
find "$name" -type f -print
else
echo "$name"
fi
done
除了傳遞管道echo "$variable"
,還可以使用here-string:
while IFS= read -r name
do
if [[ -d $name ]]
then
find "$name" -type f -print
else
echo "$name"
fi
done <<<"$variable"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.