Bash 写入 ls 文件错误

Bash write ls error to file

我有一个文件夹,里面有很多文件,这些文件以连续的数字和一些文本命名,但有些数字丢失了。我想将所有缺失的数字写入一个文件。

这是我目前得到的:

#!/bin/bash
for (( c=23457; c<=24913; c++ ))
do
  files=$(printf %q kassensystem/documents/"${c}")
  ret=$(ls $files*)
  echo "$ret" >> ./out.log
done

输出如下所示:

所有现有文件都写入文件,所有错误都写入控制台。我想要完全相反的方式。写入文件的所有错误(ls: ..file not found)!

我尝试使用完整的命令 ls $files* | grep -v 'kasse*',但我只得到一个包含空行的文件。

感谢您的帮助!

exec 4>out.log  # open output file just once, not once per write

for (( c=23457; c<=24913; c++ )); do
  files=( kassensystem/documents/"$c"* ) # glob into an array
  [[ -e $files ]] || echo "$c" >&4       # log if first file in array doesn't exist
done