我写了一个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