bash - Nested If else Statement, operand expected
问题描述
If else statement nested in for loop. Operand expected. error token is:
Ubuntu Linux
for i in "${dir_path[@]}"; do
if [ "$dir_path[i]" ]; then <--- this line
echo "$filename found, Directory: $i"
else
echo "$filename not found"
fi
done
It finds the paths correctly but it displays error: "operand expected (error token is "first directory"
解决方案
要遍历数组的索引,请使用
for i in "${!dir_path[@]}" ; do
# ^
要检查文件是否存在,请使用[ -f "${dir_path[i]}" ]
(对于数组,或与$i
关联数组相同)。你所拥有的总是正确的,因为它检查字符串"$dir_path[i]"
是否不为空,它永远不会是,因为它总是至少包含[i]
.
推荐阅读
- pdf - 正确从 PDF 中提取图像
- typescript - 为什么我收到错误无法解析模块的路径?带有 Typescript 的 Eslint
- c# - 使用 LINQ 从 db 中选择不同的列名
- image - 如何从 sqlite3 检索图像(BLOB 数据)到 Tkinter 窗口?
- url - TYPO3 v10.4.9 新闻列表视图 URL-配置
- wordpress - 在 wordpress 自定义帖子类型库中添加多个图像
- go - 如何用另一个公钥加密私钥?
- node.js - Node.js:检查是否存在任何类型的文件系统条目,包括:损坏的符号链接(不能使用 fs.existsSync)
- bounding-box - JSXGraph:在 for 循环中使用边界框大小
- javascript - 从 HTML 元素生成 JSON 数据