【问题标题】:How do I grab files that don't exist and store them into a variable? [duplicate]如何获取不存在的文件并将它们存储到变量中? [复制]
【发布时间】:2016-01-15 14:52:15
【问题描述】:

我希望当前目录中存在 6 个文件:

prefix_alpha.txt
prefix_beta.txt
prefix_gamma_.txt
prefix_delta.txt
prefix_epsilon.txt
prefix_feta.txt

如何获取不存在的所有文件的列表并将它们存储到 Bash 中的变量中?如果不存在超过 1 个,则只希望文件名之间有一个空格。结束操作是使用 SQL 语句中的文件名,如INSERT INTO table (columnname) VALUES ("File(s) '$notexistingfiles' don't exist")

【问题讨论】:

标签: bash


【解决方案1】:

您可以使用test(7) 命令检查文件是否存在,因此您所要做的就是遍历预期的文件,如果它们不存在,则将它们添加到数组中。

# The target files
FILES=(
    "prefix_alpha.txt"
    "prefix_beta.txt"
    "prefix_gamma_.txt"
    "prefix_delta.txt"
    "prefix_epsilon.txt"
    "prefix_feta.txt"
)

# Create a missing array
declare -a MISSING

for file in "${FILES[@]}"; do
    test -e "$file" || MISSING+=("$file")
done

echo "${MISSING[@]}"

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    相关资源
    最近更新 更多