[英]How do you loop through your current directory and store files in an array? Bash shell script
我對編程很陌生,對BASH則完全陌生。 如標題中所述,我試圖遍歷當前目錄並將以.cpp結尾的文件存儲到數組中。 我也試圖創建第二個數組,用“ .o”替換“ .cpp”后綴,每當我嘗試編譯時,我都會收到“條件語句中的語法錯誤”
x=0
cwd=$(pwd)
for i in $cwd; do
if [[ $i == *.cpp]]
then
cppfield[$x] = $i
ofield[$x] = field[$x] | sed s/.cpp/.o/
x=$((count+1))
fi
done
采用:
shopt -s nullglob # In case there are no matches
for i in *.cpp; do
...
done
在您的代碼中,您只是將i
設置為$cwd
,而不是目錄中的文件。
我不確定您這樣做的目的是什么。 但是,如果您只想生成將.cpp替換為.o的文件名,則可以用一種更簡單的方法來完成
for f in *.cpp
do
echo ${f/.cpp/.o}
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.