gpt4 book ai didi

c++ - 您如何遍历当前目录并将文件存储在数组中? Bash 脚本

转载 作者:行者123 更新时间:2023-11-28 02:33:58 25 4
gpt4 key购买 nike

我对编程很陌生,对 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,而不是目录中的文件。

关于c++ - 您如何遍历当前目录并将文件存储在数组中? Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com