3
我寫了一個bash文件,其中使用了read
命令從文件中讀取數據。如何在shell腳本中保存「讀取」錯誤輸出?
如果文件不存在,我想將錯誤保存到文本文件中。我想:
read myVariable < myFile 2> errorFile.txt
它不工作,和許多其他的努力zhcon失敗,例如:
myVar=`read myVariable < myFile`
我寫了一個bash文件,其中使用了read
命令從文件中讀取數據。如何在shell腳本中保存「讀取」錯誤輸出?
如果文件不存在,我想將錯誤保存到文本文件中。我想:
read myVariable < myFile 2> errorFile.txt
它不工作,和許多其他的努力zhcon失敗,例如:
myVar=`read myVariable < myFile`
首先,您需要重定向STDERR告訴你的bash從沒有一個文件的讀取之前存在
這會爲你工作:
$ read myVariable 2> errorFile < myFile
或
$ 2> errorFile.txt read myVariable < myFile
是的,它的工作原理,謝謝。你能告訴我爲什麼我應該重定向STDERR首先閱讀? – 2012-02-14 09:37:16
不必在'read'之前,就在你讓bash打開一個不存在的文件之前。查看更新的答案 – SiegeX 2012-02-14 09:56:24