linux - 如何在 shell 脚本中保存 "read "错误输出?

标签 linux shell

我编写了一个 bash 文件,其中我使用 read 命令从文件中读取数据。

如果文件不存在,我想将错误保存到文本文件中。我试过:

read myVariable < myFile  2> errorFile.txt

它不起作用,许多其他努力都失败了,例如:

myVar=`read myVariable < myFile`

最佳答案

在告诉 bash 从不存在的文件中读取之前,您需要先重定向 STDERR

这对你有用:

$ read myVariable 2> errorFile < myFile

$ 2> errorFile.txt read myVariable < myFile

关于linux - 如何在 shell 脚本中保存 "read "错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273958/

相关文章:

linux - Bash:如何输出 ls -la 的 "grep"匹配文件所有者和文件权限?

Linux zip 不维护目录结构

c++ - waitpid/wexitstatus 返回 0 而不是正确的返回码

linux - azure linux web应用程序白名单IP收到401错误

c - Linux 命令中的更多选项和编译错误

linux - 如何在 shell 中迭代文本文档的单词。我想显示文本文档中的字数

linux - 如何在 Linux bash 中检查脚本文件是否存在并在一个线程中执行它

c - 使用 proc/cpuinfo 查找vendor_id

bash - 捕获 bash exec I/O 重定向中的错误输出

bash - 如何比较重复目录之间的文件大小匹配?