0

If suppose I execute this:

mysql -umyuserid -pmypassword -hmymachine -e "select * from mytable;"

I will get my rows with borders drawn with |, - etc However, the same command when redirected to a file will get me something like a tab delimited output

mysql -umyuserid -pmypassword -hmymachine -e "select * from mytable;" > mydata.csv

This resultant file can be opened in a spreadsheet program

So my question -- how does it do it, and why was it so worth their while to do it?

gk_2000
  • 194
  • 3
  • 16

1 Answers1

2

It's not difficult to check in C if the input or output is redirected or if it's interactive:

https://github.com/mysql/mysql-server/blob/8.0/client/mysql.cc#L1273-L1274

  if (!isatty(0) || !isatty(1)) {
    status.batch = true;
    ...

You can also set "batch mode" with the --batch option when you invoke the mysql client.

See What is isatty() in C for? for an explanation of that C function.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks for the enlightenment. May I further ask: But it's it unusual, sort of going against an unwritten standard? Shouldn't a command behave exactly the same and leave the data moving to the invoker? Because if this was the way it is meant to be there was scope for certain programs to leave out the screen coloration info when outputting to a file and so on but I never encountered such implementation. – gk_2000 Sep 24 '22 at 06:38
  • In this case, the mysql client is working exactly as written in the manual. Read starting at: _"The default output format is different (more concise) when you run mysql in batch mode than when you use it interactively."_ in this manual page: https://dev.mysql.com/doc/refman/8.0/en/batch-mode.html – Bill Karwin Sep 24 '22 at 13:46