繁体   English   中英

如何将shell命令的结果分配给MySQL变量?

[英]How to assign the result of a shell command to a MySQL variable?

MySQL Shell中的Shell命令返回值:

mysql> \! echo 1
1

如何将此结果分配给MySQL变量? 喜欢:

mysql> set @var = \! echo 1
1
    -> ;

错误1064(42000):您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法

问题是SET是服务器命令,它更改服务器上保存的变量的状态。 system (及其简写为\\! )是一个客户端命令,它在客户端上派生一个进程。 尽管客户端可以将分支过程的退出代码传输到服务器以存储在用户变量中,但我认为MySQL命令行客户端不提供此功能。

mysql-5.5.29/client/mysql.cc

3958: static int
3959: com_shell(String *buffer __attribute__((unused)),
3960:           char *line __attribute__((unused)))
3961: {
3962:   char *shell_cmd;
3963: 
3964:   /* Skip space from line begin */
3965:   while (my_isspace(charset_info, *line))
3966:     line++;
3967:   if (!(shell_cmd = strchr(line, ' ')))
3968:   {
3969:     put_info("Usage: \\! shell-command", INFO_ERROR);
3970:     return -1;
3971:   }
3972:   /*
3973:     The output of the shell command does not
3974:     get directed to the pager or the outfile
3975:   */
3976:   if (system(shell_cmd) == -1)
3977:   {
3978:     put_info(strerror(errno), INFO_ERROR, errno);
3979:     return -1;
3980:   }
3981:   return 0;
3982: }

请特别注意, system(3)调用的返回值未存储在任何地方。 但是,如果进程使用该代码退出,则com_shell()函数调用将返回-1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM