2017-02-16 73 views
1

我正在寻找从我写的SQL中的“count”变量中返回一个数值。不幸的是,我只是得到一个安格尔错误消息。任何想法我做错了什么?在shell脚本中将表计数设置为变量..?

见shell脚本代码如下:

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

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

echo "Table count = $count" 

见安格尔错误如下:

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 
+0

如果您使用的是Korn shell中,使用'ksh'标签,而不是'bash'标签。无论如何,这不是一个壳问题。你的SQL命令有问题。 – chepner

+1

您是否尝试过执行'sql db_name -s-N“SELECT COUNT(*)FROM temp_table;”'查看脚本之外的输出是什么? – Fred

回答

1

试试这个:

=>|Fri Feb 17|01:51:01|[email protected][STATION]:/var/lib/pgsql> ./test.sh 
count ------- 3 (1 row) 

=>|Fri Feb 17|01:51:04|[email protected][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|[email protected][STATION]:/var/lib/pgsql> 
+0

结果现在是:。 “INGRES监控终端版权所有2008 Ingres公司Ingres的SPARC Solaris版本9.2.1 II登录周四4月20日11点30分四十秒2017年继续执行+ ----------- - + | col1 | + ------------- + | 8 | + ------------- +(1 row)你的SQL语句有Ingres Version II 9.2.1注销Thu Apr 20 11:30:40 2017“ 任何想法如何才能返回”8“? – TCP

+0

您可以在这里分享截图吗?然后我可以检查并回复。 – User9102d82