1

my question maybe quite straightforward or may have no solution. So, I ask you to kindly help me finding out how to access information of the stdout redirect to out.txt

node do-nothing.js < in.txt > out.txt

In particular I need to access the filename "out.txt" in the script do-nothing.js (i.e. accessing process.stdout.______)

Tips: I've already noticed that process.stdout._type become 'fs' instead of 'tty' while applying a redirection.

fiorentinoing
  • 948
  • 12
  • 20
  • It's impossible, and you shouldn't access that information even if it were possible. stdout can be redirected to any stream (including a tty), not necessarily a file, and that's the point. You should treat it as a write-only character device, if you support stdout output at all. – 4ae1e1 Oct 31 '15 at 23:35
  • @4ae1e1 I understand your point, but this is crucial in case I use node.js as a command-line text-stream processor of binary files (I put reference to binary file absolute path inside the text file) – fiorentinoing Nov 01 '15 at 00:08
  • I have to add that what I'm looking for is a functionality similar to [ftello](http://www.gnu.org/software/libc/manual/html_node/File-Positioning.html) in Node.js before starting to write my own module. – fiorentinoing Nov 01 '15 at 00:28
  • 1
    Then you shouldn't output to stdout. You should use command line arguments to take output filename. Simple as that. – 4ae1e1 Nov 01 '15 at 00:28
  • no single solution for all operating systems. http://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c – damphat Nov 01 '15 at 00:28
  • @damphat He may get the filename for the descriptor when it is associated to a file at all, but point is he shouldn't. He's printing to stdout, and he shouldn't assume it is seekable. – 4ae1e1 Nov 01 '15 at 00:31
  • @4ae1e1 I just would like to avoid to fill my $DATAPATH with `(new Date()).getTime()+'.bin@'` instead of meaningful filename as `out.bin@` coming from piping to out.txt file. (the problem is that legacy framework as seismic unix and rsf/madagascar work like that) – fiorentinoing Nov 01 '15 at 00:44
  • Sorry, I don't know the JS nor the legacy part (I came from the bash tag), but think about user experience. What do you do when your stdout is not directed to a file? Print an error? Horrible UI. Why not require a file upfront when it is indeed required? – 4ae1e1 Nov 01 '15 at 00:52
  • Also, things are not exposed for a reason. Treat opaque things as opaque. That's why we have abstraction. – 4ae1e1 Nov 01 '15 at 00:53
  • To process terabytes of binary data with the simplicity of a bash terminal line is a great User eXperience. Anyway, thanks for your points. – fiorentinoing Nov 01 '15 at 01:01
  • As @damphat has correctly pointed out, the solution I'm looking for isn't portable [original function of Madagascar framework](https://github.com/ahay/src/blob/master/api/c/file.c#L506). This is ok to me, anyway. – fiorentinoing Nov 01 '15 at 01:08

2 Answers2

0

You can use this modulehttps://www.npmjs.com/package/yargs and then this module you named https://www.npmjs.com/package/fs in the other hand if you need something more complex you can use child process package to do what you need from args and execute a bash script you have for example

juan garcia
  • 1,326
  • 2
  • 23
  • 56
  • This is not applicable because I don't pass the filename as arguments of node. The result of `node yargs.js < in.rsf` is in fact `{ _: [], '$0': 'yargs.js' }` – fiorentinoing Nov 01 '15 at 00:05
  • You are right that's the result of node yargs.js < in.rsf . I thought you may want need to do something like node inout.js streaming streamout and inout.js uses yargs and process child to redirect what you need. Also in process child you could execute a bash too... Hope it helpa – juan garcia Nov 01 '15 at 00:17
-1

I got it. Here is the solution, if you launch the script as node stdout_redirect.js > out.txt && more out.txt you have the sought information in the d variable. Thanks for help and hope this help someone else.

var fs = require('fs');

fs.readdir(process.env.PWD,function(err, dir){
    var stdout_ino = 0;
    fs.stat('/dev/stdout',function(err,stats){
        stdout_ino = stats.ino;
        dir.forEach(function(d){
            fs.stat(d,function(err,stats){
                if (stats.ino === stdout_ino)
                {
                    console.log(d);
                }
            });
        });
    });
});
fiorentinoing
  • 948
  • 12
  • 20
  • That's not very general though, but it will indeed work for files in the working directory... – masterxilo Nov 26 '18 at 22:38
  • @masterxilo why didn't you propose a better solution? it works like a charme to me, though. the real problem is the concurrency issues the proposed solution had. Now fixed. – fiorentinoing Jan 02 '19 at 11:00