Let us say I redirect the STDOUT, STDERR of a program to files.
./script.sh 1> output.log 2> error.log
Can the running program find this out i.e. know the paths to these files?
|
Let us say I redirect the
Can the running program find this out i.e. know the paths to these files? |
||||
|
|
|
You can call
If the file name doesn't contain a newline, you'll be able to get away with Note that the file name may not always exist, in particular if the file has been deleted. Under Linux, another way to access the file names is through It is usually a very bad idea to make any use of the information obtained this way. If someone calls your script with the output redirected to a file, they won't like it if you behave differently because of the location of the file, or affect the file in ways other than appending to it. There is one exception: you may want to react differently depending on whether you're writing to a terminal or to something else (pipe, socket, file), for example to display colors or progress indicators on a terminal. There is a specific test to determine whether a file descriptor is connected to a terminal:
|
|||||||||||||||||||||
|
OUTPUT:
OUTPUT:
|
|||||||||||||||||||||
|
|
In your case, If you want your program "know the path to these files", you must use absolute path:
|
|||
|
|
|
The
|
|||
|
|