我有以下bash stript打印出project.json
文件路径:
projectDirectories=./src/*/
projectJsonFiles=${projectDirectories%%/}/project.json
for projectFilePath in $projectJsonFiles; do echo $projectFilePath; done
给了我这个输出:
./src/foo-aspnet-configuration/project.json
./src/foo.Common/project.json
./src/foo.bar.MongoDB/project.json
./src/foo.bar/project.json
./src/foo.bar.Queries/project.json
./src/foo.Graph/project.json
./src/foo-http-frontend/project.json
./src/foo.Http.Model/project.json
./src/foo.Infrastructure/project.json
./src/foo.Search/project.json
我还想做的是列出./workers/*/
路径下的project.json文件,但我不知道如何组合它。我的最终目标是能够将两个文件整合在一起,例如:
projectDirectories=./src/*/,./workers/*/
projectJsonFiles=${projectDirectories%%/}/project.json
for projectFilePath in $projectJsonFiles; do echo $projectFilePath; done
会给我:
./src/foo-aspnet-configuration/project.json
./src/foo.Common/project.json
./src/foo.bar.MongoDB/project.json
./src/foo.bar/project.json
./src/foo.bar.Queries/project.json
./src/foo.Graph/project.json
./src/foo-http-frontend/project.json
./src/foo.Http.Model/project.json
./src/foo.Infrastructure/project.json
./src/foo.Search/project.json
./workers/foo.Sync.Common/project.json
./workers/foo-sync-foobar/project.json
./workers/foo-sync-bar/project.json
但显然,projectDirectories=./src/*/,./workers/*/
不起作用。知道如何在bash中完成这项工作吗?
答案 0 :(得分:2)
使用GNU bash 4:
printf -v projectDirectories "%s" ./src/*/ ./workers/*/
echo "$projectDirectories"
答案 1 :(得分:1)
您应该使用数组而不是依赖于不包含空格的文件路径:
dirs=(./src/*/ ./workers/*/)
for dir in "${dirs[@]}"; do
file=${dir}project.json; do
echo "$file"
done
done