![](/img/trans.png)
[英]Bash script for searching of files (file types) which are defined in the external file recursively in directory using find command
[英]Using find command in bash in order to find multiple files with different types
我正在運行測試方案。 每次執行場景時,特定目錄中都有兩個報告文件。 一個是文本文件,另一個是HTML文件。 我想制作一個索引文件以鏈接到所有文件。 我編寫了一個for循環來遍歷場景文件並執行它們; 我還想從HTML文件中讀取測試場景的最終結果,並將其附加到索引文件中。 在循環的最后,我使用find命令為鏈接添加<a>
標簽。
for line in $(grep '^scenario ' $scenarioList | cut -d' ' -f2)
do
# Scripts for running tests
find -name '*.txt' -exec sh -c 'f="`basename {}`"; echo "<br><span>Text Report: <a href="./$f" target="_blank">$f</a> -- </span>" >> index.htm' \;
find -name '*.html' -exec sh -c 'f="`basename {}`"; p=`cat $f | grep -Eo "Final Result :.*\." | cut -d"." -f1`; echo "<span>HTML Report: <a href="./$f" target="_blank">$f</a></span> -- <span>$p</span><br>" >> index.htm' \;
# Other scripts
done
它應在一行中以--
分隔創建到文本文件的鏈接,到HTML文件的鏈接以及每個方案的最終結果。
如果我在單個場景中運行此腳本,那么一切似乎都正確:
但是,如果我在更多情況下運行它,則會創建錯誤的鏈接:
我知道我可以使用-o
選項進行邏輯或,但是我不知道如何將文本文件和HTML文件彼此分開來創建鏈接。 任何幫助,將不勝感激。
您使用find
的-o
和-printf
選項
find . -name '*.txt' -printf '<br><span>Text Report: <a href="%p" target="_blank">%f</a> -- </span>' -o -name '*.html' -printf '<span>HTML Report: <a href="%p" target="_blank">%f</a></span>'
我可能在這里弄亂了您的格式,但我認為您明白了。
%p選項打印相對於搜索的完整文件路徑,%f打印文件名
也許像這樣。 這假定可以根據HTML文件的名稱來預測文本文件的名稱。 我還重構了您的shell腳本,使它看起來很簡單和/或效率低下。
grep '^scenario ' "$scenarioList" |
cut -d' ' -f2 |
while read -r line; do
# Scripts for running tests
find -name '*.html' -exec sh -c '
f="$(basename {} _style-all.html)";
echo "<br><span>Text Report: <a href="./${f}blog.txt" target="_blank">${f}blog.txt</a> -- </span>";
h=$(grep -Eo "Final Result :.*\." {} | cut -d"." -f1);
echo "<span>HTML Report: <a href="./${f}_style-all.html" target="_blank">${f}_style-all.html</a></span> -- <span>$p</span><br>"'
# Other scripts
done >index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.