msys / mingw command line requires exe extension if stdout is redirected

The underlying cause of this problem is described elsewhere, with partial workarounds provided.

For example: stdout is not a tty and stdin is not a tty

An example of a command line I'm having problems with in MSYS2 or MINGW64 environments is this:

# psql -c '\d' | grep public
stdout is not a tty

Here's the output, if issued without piping to grep:

# psql -c '\d'
               List of relations
 Schema |      Name      |   Type   |  Owner
--------+----------------+----------+----------
 public | tagname        | table    | postgres
 public | tagname_id_seq | sequence | postgres
(2 rows)

In order to redirect stdout, it's apparently necessary to edit the command line, changing psql to psql.exe. This modification does the trick:

# psql.exe -c '\d' | grep public
 public | tagname        | table    | postgres
 public | tagname_id_seq | sequence | postgres

This version works, whether or not stdout is redirected:

# psql.exe -c '\d'
               List of relations
 Schema |      Name      |   Type   |  Owner
--------+----------------+----------+----------
 public | tagname        | table    | postgres
 public | tagname_id_seq | sequence | postgres
(2 rows)

Note that the problem only exists for some programs. For example, /usr/bin/find is indifferent to whether the .exe extension is specified. Also, the cygwin version of psql.exe does not suffer from this limitation.

The workaround of appending .exe could be hidden with an alias if you could always call psql as psql.exe. However, if you need an interactive session, problems can occur when using the .exe extension. NOTE: there are multiple versions of psql.exe installable via pacman and this may not apply to all of them.

So my question is this: is it possible to create a wrapper program (for example, in golang or C, or a shell script) to hide this problem? The goal would be to support optionally redirectable command lines without requiring the .exe extension.

Assuming the wrapper is named "fixtty", it would spawn a command line that could be redirected or not. In other words, neither of these command lines would fail with an error message:

# fixtty psql -c '\d'
# fixtty psql -c '\d' | grep public

Note that it would not need to handle interactive programs (like psql) because they work fine as long as the .exe extension is not provided.



from Recent Questions - Stack Overflow https://ift.tt/3mKEbpB
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation