Shell command inside MySQL shell returns value:
mysql> \! echo 1
1
How to assign this result to MySQL variable? Like:
mysql> set @var = \! echo 1
1
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1
The problem is that
SETis a server command, which changes the state of variables held on the server; whereassystem(and its\!shorthand) is a client command, which forks a process on the client. Whilst it could be possible for the client to transmit the forked process’s exit code to the server for storage in a user variable, I don’t think the MySQL command line client offers this functionality.From
mysql-5.5.29/client/mysql.cc:Note in particular that the return value of the
system(3)call is not stored anywhere; however thecom_shell()function call will return-1if the process exited with that code.