bash 文件中的 sqlcmd - 存储到变量 [英] sqlcmd in bash file - store to variable

查看:26
本文介绍了bash 文件中的 sqlcmd - 存储到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 docker 容器中运行,我试图确定 mssql 数据库中是否存在表:

Running in a docker container, I am trying to determine if a table exits in the mssql database:

RESULT='/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -d master -i "SELECT name FROM master.sys.databases WHERE name = N'MyTable'"'
    if [ "$RESULT" == "MyTable" ]; then
        echo YES
    fi
    echo "$RESULT"

echo "$RESULT" 总是将整个命令作为字符串输出,不会被执行.所以它只是将它分配为一个刺...

echo "$RESULT" always just outputs the entire command as a string, its not getting executed. So its just assigning it as a sting...

如何执行并分配结果?

推荐答案

Bash 不执行用单引号定义的命令.单引号用于字符串定义.

Bash does not execute commands defined in single quotes. Single quotes are used for string definitions.

您尝试执行的操作称为命令替换.它可以通过两种不同的方式完成:

What you try to do is called command substitution. And it can be done in two different ways:

RESULT=`command` 
RESULT=$(command)

所以你的例子应该是这样的:

So your example should look like this:

RESULT=`/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -d master -i "SELECT name FROM master.sys.databases WHERE name = 'MyTable'`
if [ "$RESULT" == "MyTable" ]; then
    echo YES
fi
echo "$RESULT"

您可以在此处找到有关命令替换的更多信息:https://www.gnu.org/software/bash/手册/html_node/Command-Substitution.html

You can find more information regarding command substitution here: https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

这篇关于bash 文件中的 sqlcmd - 存储到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆