arrays - Bash 循环遍历多个目录
问题描述
我不知道是否达到了这个话题,或者机器人找不到任何相关的东西。
我有一个 bash 脚本,我想在多个目录中执行 for 循环,并提到我只想在日志文件中输出,*.txt/files
但循环不要在子目录中进行。
我的愿望是使用变量中的目录。我正在使用一个数组,我在其中编写了我搜索它们的目录。运行脚本时,输出是数组中的内容,而不是目录中的内容...
这就是我的代码现在的样子,我该怎么办?:
#!/bin/bash
l=/home/Files/Test1/
j=/home/Files/Test2/
k=/home/Files/Test3/
arr=("$l" "$j" "$k")
for i in "${arr[*]}"
do
echo "$i" >> test
done
感谢您的任何帮助!
解决方案
只是find
实际的文件。
find "${arr[@]}" -maxdepth 1 -type f >> test
您可以依赖 shell 文件名扩展:
for dir in "${arr[@]}" # properly handle spaces in array values
do
for file in "$dir"/*; do
# check for empty dir
if [ -f "$file" ]; then
# Use printf, in case file is named ex. `-e`
printf "%s\n" "$file"
fi
done
# don't reopen the file on each loop, just open it once
done >> test
但还有很多,仅find
此而已。
推荐阅读
- json - 如何使用显式模式映射 openjson 查询中的键?
- arrays - 打字稿:将帖子请求正文转换为地图
- github - GitHub Linguist 的 language.yml 是否允许在扩展中使用通配符?
- python - 是否可以通过 doctest 测试使用 get_type_hints 的函数?
- c# - C# - 如何在 Linq 中执行 SUM() / SUM()
- c++ - 你能安全地在 Lua CFunction 中弹出函数参数吗?
- automated-tests - 空手道; 单个场景中的多个 url
- google-cloud-dataflow - 如何使用 Apache Beam Python SDK 在两组 json 存储桶上执行“差异”?
- r - 如何修复“FUN中的错误(X [[i]],...):运算符'<-'未实现”在R中
- c# - azure 服务总线的 Azure Function App 延迟重试