macro: Redirection in exec( UNIX-binary ) ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

macro: Redirection in exec( UNIX-binary ) ?

Gluender-4
Sorry to bother the list once more...

Is it possible to use "redirection" when executing an UNIX-binary from a macro?

For example from OSX-Terminal I write

        MyMac:~ me$ theBinary --output=theFile.png < /pathTo/myData

with input file "/pathTo/myData".

 From an ImageJ macro

        exec( "theBinary", "--output=theFile.png", "<", "/pathTo/myData" );

doesn't do the trick.

Is there a way to redirect standard input to an input file with exec( )?

Best
--

                   Herbie

          ------------------------
          <http://www.gluender.de>
Reply | Threaded
Open this post in threaded view
|

Re: macro: Redirection in exec( UNIX-binary ) ?

dscho
Hi,

On Tue, 27 Jul 2010, Gluender wrote:

> Is it possible to use "redirection" when executing an UNIX-binary from a
> macro?

No. You will have to employ at least Javascript to execute an external
program and catch its output.

Being on a Unix-like system, you can write a small wrapper script that
calls the executable with the appropriate redirection, though.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: macro: Redirection in exec( UNIX-binary ) ?

Michael Schmid
In reply to this post by Gluender-4
Hi Herbie,

there is a trick to do it, using the shell with '-c commandstring'
e.g.
   exec("/bin/sh", "-c", "pwd >/tmp/test");
writes the output of the 'pwd' command to /tmp/test

The -c switch works at least with bash, I am not sure about the  
original Bourne shell.

Michael
________________________________________________________________

On 27 Jul 2010, at 22:07, Gluender wrote:

> Sorry to bother the list once more...
>
> Is it possible to use "redirection" when executing an UNIX-binary  
> from a macro?
>
> For example from OSX-Terminal I write
>
> MyMac:~ me$ theBinary --output=theFile.png < /pathTo/myData
>
> with input file "/pathTo/myData".
>
> From an ImageJ macro
>
> exec( "theBinary", "--output=theFile.png", "<", "/pathTo/myData" );
>
> doesn't do the trick.
>
> Is there a way to redirect standard input to an input file with exec
> ( )?
>
> Best
> --
>
>                   Herbie
>
>          ------------------------
>          <http://www.gluender.de>
Reply | Threaded
Open this post in threaded view
|

Re: macro: Redirection in exec( UNIX-binary ) ?

dscho
Hi,

On Wed, 28 Jul 2010, Michael Schmid wrote:

> there is a trick to do it, using the shell with '-c commandstring'
> e.g.
>  exec("/bin/sh", "-c", "pwd >/tmp/test");
> writes the output of the 'pwd' command to /tmp/test

Good call! (Even if it forks two processes instead of one.)

You now have to watch out to quote arguments containing whitespace,
though.

> The -c switch works at least with bash, I am not sure about the original
> Bourne shell.

From http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html we
can see that all POSIX-conforming shells support both the '-c' option and
redirection.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: macro: Redirection in exec( UNIX-binary ) ?

Gluender-4
In reply to this post by Michael Schmid
Great list members,

in this case special thanks goes to Michael Schmid and Johannes Schindelin.

I think this valuable tip is worth being included with the macro
functions manual, no?

The reason is that quite some useful UNIX-Binaries work with stdin or
stdout only.


>Hi Herbie,
>
>there is a trick to do it, using the shell with '-c commandstring'
>e.g.
>   exec("/bin/sh", "-c", "pwd >/tmp/test");
>writes the output of the 'pwd' command to /tmp/test
>
>The -c switch works at least with bash, I am not sure about the
>original Bourne shell.
>
>Michael
>________________________________________________________________
>
>On 27 Jul 2010, at 22:07, Gluender wrote:
>
>>Sorry to bother the list once more...
>>
>>Is it possible to use "redirection" when executing an UNIX-binary
>>from a macro?
>>
>>For example from OSX-Terminal I write
>>
>> MyMac:~ me$ theBinary --output=theFile.png < /pathTo/myData
>>
>>with input file "/pathTo/myData".
>>
>>From an ImageJ macro
>>
>> exec( "theBinary", "--output=theFile.png", "<", "/pathTo/myData" );
>>
>>doesn't do the trick.
>>
>>Is there a way to redirect standard input to an input file with exec( )?
>>
>>Best Herbie



Thanks again to the IJ-heroes of the day and to Wayne for ImageJ !

Best
--

                   Herbie

          ------------------------
          <http://www.gluender.de>