[英]How to loop through files in a directory having a certain word in filename using bash script?
[英]Script to loop through files except certain files
我試圖遍歷目錄中的所有HTML
文件。
以下工作就可以了。
for f in *.html; do echo $f; done
但是,如何添加if
條件,以便僅在不等於index.html
文件名?
它應該像這樣簡單:
for f in *.html
do
if [ "$f" != "index.html" ]
then
echo $f
fi
done
for f in *.html; do [ "$f" != "index.html" ] && echo "$f"; done
也可以通過擴展的globbing從列表中完全排除index.html
:
shopt -s extglob nullglob
for f in !(index).html; do
echo "$f"
done
shopt -s extglob
:啟用擴展的shopt -s extglob
shopt -s nullglob
:確保在沒有匹配文件的情況下不執行循環 !(index).html
:擴展到所有不是index.html
html
文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.