如何让 bash 的 `cat` 程序处理 [没有这样的文件或目录] 边缘情况?

How to get bash's `cat` program to handle [No such file or directory] edge cases?

使用 mvrm 我会使用 -f 标志,但我确定如何处理 cat.

有办法吗?可以处理这种边缘情况的替代方案?还是可以处理这种边缘情况的单行 try/except?

例如,

cat path/to/potential_files/*.fa > output.fa

cat: 'path/to/potential_files/*.fa': No such file or directory

您可以使用 globbing 遍历匹配 .fa 的所有文件,并使用 >> 追加到输出文件:

for file in "$YOUR_PATH/*.fa"; do
    cat "$file" >> outout_file.txt
done

你可以用

写一个try ... catch ...
cat file 2>/dev/null ||
  { 
    echo 'Catch block, only reached when $? -ne 0'
    echo 'Another catch statement'
  }
# Or oneliner (note the spaces around the curly braces and the semicolons)
cat file 2>/dev/null || { echo 'Something '; echo 'else'; }