bash - 一个 if 条件中的 AND-OR 条件
问题描述
我有 3 个文件
File1=abc.txt File2=def.txt File3=xyz.txt
if [ -f $File1 ] && [ -f $File2 ] && [ -f $File3 ]
then
#run some command
否则,如果 3 个文件中的任何一个不存在,它应该告诉我们它是哪个文件。如果它是 2 个不存在的文件,那么它应该告诉这两个文件的名称。
任何人都知道如何做到这一点?
解决方案
您可以轻松地将缺少的那些收集到一个数组中,然后从那里取出。
missing=()
for file in abc.txt def.txt xyz.txt; do
test -e "$file" && continue
missing+=("$file")
done
if (("${#missing[@]}" == 0 )); then
# run some command
else
echo "$0: missing: ${missing[@]}" >&2
fi
如果您希望它是单行的,请将其重构为一个函数;有关简单演示,请参阅https://ideone.com/Oq7cUy
推荐阅读
- javascript - 在 Javascript 中使用正则表达式容纳多个值
- php - Laravel,调用未定义函数 Database\Seeders\factory()
- javascript - 拆除 Bridge.NET
- c# - 当我通过 C# 代码下载它时,我得到了错误的文件
- java - 如何从条带的连接帐户表单重定向回应用程序
- spring - 将数据从实体导入 DTO 时如何避免不需要的查询休眠查询数据
- r - 绑定多个 csv 文件并对其执行逻辑回归
- python - python 3.8 - 初始化时动态设置类的属性
- jquery - 在 jQuery 中使用随机数的 div 动态检测页面滚动上的当前 div
- javascript - 如何遍历多个对象并将键值与字符串进行比较