简体   繁体   English

如何在 bash 脚本中执行 docker sqlplus 查询?

[英]how to execute docker sqlplus query inside bash script?

On Ubuntu 21.04, I installed oracle dtb from docker, works fine I am using it for sql developer but now I need to use it in shell script. On Ubuntu 21.04, I installed oracle dtb from docker, works fine I am using it for sql developer but now I need to use it in shell script. I am not sure if it can work this way, or is it some better way.我不确定它是否可以以这种方式工作,或者是否有更好的方式。 I am trying to run simple script:我正在尝试运行简单的脚本:

#!/bin/bash
SSN_NUMBER="${HOME}/bin/TESTS/sql_log.txt"
select_ssn () {
        sudo docker exec -it oracle bash -c "source /home/oracle/.bashrc; sqlplus username/password@ORCLCDB;" <<EOF > $SSN_NUMBER
        select SSN from employee
        where fname = 'James';
        quit
EOF
}

select_ssn

After I run this, nothing happens and I need to kill the session.运行此程序后,没有任何反应,我需要终止 session。 or或者

the input device is not a TTY输入设备不是 TTY

is displayed被展示

Specifying a here document from outside Docker is problematic.从 Docker 外部指定 here 文档是有问题的。 Try inlining the query commands into the Bash command line instead.尝试将查询命令内联到 Bash 命令行中。 Remember that the string argument to bash -c can be arbitrarily complex.请记住, bash -c的字符串参数可以任意复杂。

docker exec -it oracle bash -c "
    source /home/oracle/.bashrc
    printf '%s\n' \\
        \"select SSN from employee\" \\
        \"where fname = 'James';\" |
    sqlplus -s username/password@ORCLCDB" > "$SSN_NUMBER"

I took out the sudo but perhaps you really do need it.我拿出了sudo但也许你真的需要它。 I added the -s option to sqlplus based on a quick skim of the manual page.我根据手册页的快速浏览向sqlplus添加了-s选项。

The quoting here is complex and I'm not entirely sure it doesn't require additional tweaking.这里的引用很复杂,我不完全确定它不需要额外的调整。 I went with double quotes around the shell script, which means some things inside those quotes will be processed by the invoking shell before being passed to the container.我在 shell 脚本周围使用了双引号,这意味着这些引号中的某些内容将在传递给容器之前由调用 shell 处理。

In the worst case, if the query itself is static, storing it inside the image in a file when you build it will be the route which offers the least astonishment.在最坏的情况下,如果查询本身是 static,则在构建时将其存储在文件中的图像中将是提供最少惊喜的路径。

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

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