2

I try to write a wrapper script for the mysql console client. While working on this, I stumbled upon zpty. It seems perfect.

My first goal was to retrieve some table contents, but I already fail doing this. There is some really strange behaviour.

If I run zsh and type in myself

$ zmodload zsh/zpty
$ zpty -b MYSQL mysql DATABASE-NAME
$ zpty -r MYSQL

everything works as expected and

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 448
Server version: 5.5.54-0+deb8u1 (Debian)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [DATABASE-NAME]> %

is printed.

If I put the same lines of code in a shellscript

#!/bin/zsh
zmodload zsh/zpty
zpty -b MYSQL mysql DATABASE-NAME
zpty -r MYSQL

and execute it, nothing is printed.

If I remove the -b in the shellscript

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 449
Serv

is printed. Then the script freezes.

In general I notice some very incoherent behaviour. Is this a bug? What am I doing wrong?

  • mysql is scriptable. You shouldn't need to use things like expect/zpty. In any case with -b (non-blocking), the zpty -r will return nothing unless you give mysql some time to write something beforehand. Which explains your behaviour. – Stéphane Chazelas Mar 13 '17 at 14:34
  • Not if I want to do BEGIN; STUFF; transactions and ask the user whether he wants to commit or rollback, as far as I know. This is one of the usecases I am preparing this for – Michael Palm Mar 13 '17 at 14:38
  • 1
    OK. I see your point. Then you'd need to wait for the mysql prompts (see the zpty -r variant with a PATTERN or read the output line-by-line). You'd probably be better off using expect or similar that can do more out of the box. At which point you may want to consider using a programming language with a proper mysql API instead. – Stéphane Chazelas Mar 13 '17 at 14:53
  • See also the --pager command in mysql which you might be able to use to interact with mysql via a named pipe. (--pager='cat > some-fifo', send commands via a pipe to mysql and read the results via some-fifo) – Stéphane Chazelas Mar 13 '17 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.