gk_2000
gk_2000

Reputation: 351

How does MySql figure out that its output is being captured to a file?

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?

Upvotes: 0

Views: 30

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562230

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.

Upvotes: 2

Related Questions