
這應該是個比較簡單的問題。我試圖在循環中透過可執行程式傳遞多個檔案。我嘗試了多種策略(儲存在數組中等),但文件名似乎沒有被識別為變數。我收到的錯誤是:沒有這樣的檔案或目錄。
我最近嘗試的概述:
FILES=path/*/*
for f in $FILES
do
./function -input $f
done
我正在 bash shell 中工作。
答案1
建議使用適當的數組並啟用 shell 通配選項globstar
來填充所有子目錄中的文件,而不是使用普通變數來儲存全域擴展序列
shopt -s globstar nullglob
file_list=(path/**/*)
並使用數組循環來處理每個文件
for file in "${file_list[@]}"; do
printf '%s\n' "$file"
# Your other actions on file goes here
done