Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 我怎样才能保存;改为;shell脚本中的错误输出?_Linux_Shell - Fatal编程技术网

Linux 我怎样才能保存;改为;shell脚本中的错误输出?

Linux 我怎样才能保存;改为;shell脚本中的错误输出?,linux,shell,Linux,Shell,我编写了一个bash文件,其中使用read命令从文件中读取数据 如果文件不在那里,我想将错误保存到文本文件中。我试过: read myVariable < myFile 2> errorFile.txt read myVariableerrorFile.txt 它不起作用,许多其他努力都失败了,例如: myVar=`read myVariable < myFile` myVar=`readmyvariableerrorFileerrorFile.txt读取myVaria

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

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

read myVariable < myFile  2> errorFile.txt
read myVariableerrorFile.txt
它不起作用,许多其他努力都失败了,例如:

myVar=`read myVariable < myFile`
myVar=`readmyvariable
在告诉bash读取不存在的文件之前,需要先重定向STDERR

这将对您有用:

$ read myVariable 2> errorFile < myFile
$read myVariable 2>errorFile

$2>errorFile.txt读取myVariable
是的,很有效,谢谢。你能告诉我为什么我应该先重定向STDERR以进行读取吗?不一定要在
读取之前,就在你让bash打开一个不存在的文件之前。请参阅更新的答案
$ 2> errorFile.txt read myVariable < myFile