Reputation: 3081
I write a bash file in which i used read
command to read data from a file.
If the file wasn't there I want to save the error into a text file. I tried:
read myVariable < myFile 2> errorFile.txt
it doesn't work, and many other efforts faild such as:
myVar=`read myVariable < myFile`
Upvotes: 4
Views: 802
Reputation: 140417
You need to redirect STDERR first before you tell bash to read from a file that doesn't exist
This will work for you:
$ read myVariable 2> errorFile < myFile
or
$ 2> errorFile.txt read myVariable < myFile
Upvotes: 2