【问题标题】:Setting table count to variable in shell script..?在shell脚本中将表计数设置为变量..?
【发布时间】:2017-07-05 23:18:17
【问题描述】:

我希望在我编写的 SQL 的“count”变量中返回一个数值。不幸的是,我只是收到一条 Ingres 错误消息。任何想法我做错了什么?

查看下面的shell脚本代码:

#!/bin/ksh
###############

count=$(sql db_name -s -N "SELECT COUNT(*) FROM temp_table;")

echo "Table count = $count"

请参阅下面的 Ingres 错误:

Table count = INGRES TERMINAL MONITOR Copyright 2008 Ingres Corporation 
E_US0022 Either the flag format or one of the flags is incorrect,
    or the parameters are not in proper order.

预期结果:

Table count = 8

【问题讨论】:

  • 如果您使用的是 Korn shell,请使用 ksh 标签,而不是 bash 标签。无论如何,这不是外壳问题。你的 SQL 命令有问题。
  • 您是否尝试过执行sql db_name -s -N "SELECT COUNT(*) FROM temp_table;" 来查看脚本之外的输出?

标签: mysql bash shell variables ingres


【解决方案1】:

-N 不是 Ingres 终端监视器(sql 命令)的有效标志。 你可能想要这样的东西(在 bash 中):

count=`echo "select count(*) from iitables;\g" | sql -S iidbdb`

有关接受的标志的更多信息,请参阅文档: http://docs.actian.com/#page/Ing_CommandRef%2FCommandRef_Body.1.235.htm%23

【讨论】:

    【解决方案2】:

    试试这个:

    =>|Fri Feb 17|01:51:01|postgres@[STATION]:/var/lib/pgsql> ./test.sh
    count ------- 3 (1 row)
    
    =>|Fri Feb 17|01:51:04|postgres@[STATION]:/var/lib/pgsql> cat test.sh
    #!/bin/bash
    
    count=$(psql <<EOF
    select count(*) from mdn_2 ;
    EOF
    )
    # Prints the result captured from DB
    echo $count
    
    =>|Fri Feb 17|01:51:05|postgres@[STATION]:/var/lib/pgsql>
    

    【讨论】:

    • 结果现在是:“INGRES TERMINAL MONITOR 版权所有 2008 Ingres Corporation Ingres SPARC SOLARIS 版本 II 9.2.1 login Thu Apr 20 11:30:40 2017 continue Executing . . . +------ -------+ |col1 | +-------------+ | 8| +-------------+ (1 行) 你的 SQL声明已提交。Ingres Version II 9.2.1 logout Thu Apr 20 11:30:40 2017" 有什么想法可以返回“8”吗?
    • 可以在这里分享截图吗?然后我可以检查并回复。
    猜你喜欢
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多