bash - 语法错误: invalid arithmetic operator (error token is "

标签 bash math syntax syntax-error

我的脚本拒绝在cron下工作,但是在手动执行时可以正常工作

#!/bin/bash
LOGFILE=/opt/xxx/scripts/rc.log
fUpMail() {
echo -e "Hello!\n\n$1 xx\n\nBest regards,\n\nCheck LLC 2k18" | mailx -s "$1 Rates were not imported" smone@smth.com 
}
curDate=`date +%Y-%m-%d`
#postgres expression output being assigned to a variable
rateQ=`PGPASSWORD=xxxxxx psql -t -h xxx.xxx.228.134 -p 5433 -d axx2 -U axxx2bo << EOF
SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'
EOF`
#same for demodb
rateDemo=`PGPASSWORD=xxx psql -t -h xx.xxx.42.14 -p 5432 -d axxxo -U acxxxxbo << EOF
SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'
EOF`
#logging
printf "\n`date +%H:%M:%S` $curDate $rateQ $rateDemo\n" >> $LOGFILE

#check if rate value is not null
if [[ $(($rateQ)) != 0 ]] && [[ $(($rateDemo)) != 0 ]];
then
#posting a commentary into jira
curl -u xxx-support-bot:Rzq-xxx-xxx-gch -X POST --data '{"body": "'"$rateQ"' LIVE rates for '"$curDate"' were imported automatically'"\n"''"$rateDemo"' DEMO rates for '"$curDate"' were imported automatically"}' -H "Content-type: application/json" https://jira.in.xxx.com:443/rest/api/2/issue/xxxxxx-1024/comment >> $LOGFILE
else
#if rates were not imported
if [[ $(($rateQ)) == 0 ]];
then
echo "looks like LIVE rates for $curDate were not imported, please check manually!"
#sending a letter
fUpMail 'LIVE'
fi
if [[ $(($rateDemo)) == 0 ]];
then
echo "looks like DEMO rates for $curDate were not imported, please check manually!"
fUpMail 'DEMO'
fi
fi

cron发送以下消息:

/opt/xxx/scripts/ratecheck.sh: line 25: Timing is on.
  6543

Time: 4.555 ms: syntax error: invalid arithmetic operator (error token is ".
  6543

Time: 4.555 ms")


第25行是
if [[ $(($rateQ)) != 0 ]] && [[ $(($rateDemo)) != 0 ]];

可以请人帮忙解释一下这里出什么问题了吗?

最佳答案

您从psql获得的不仅仅是一个普通数字,而且这正在干扰您正在进行的类型转换。我认为您可以像这样删除多余的输出:

rateQ=$(PGPASSWORD=xxxxxx psql -t -h xxx.xxx.228.134 -p 5433 -d axx2 -U axxx2bo -q -c "SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'")

rateDemo=$(PGPASSWORD=xxx psql -t -h xx.xxx.42.14 -p 5432 -d axxxo -U acxxxxbo -q -c "SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'")

请注意-q标志的添加:

-q --quiet

Specifies that psql should do its work quietly. By default, it prints welcome messages and various informational output. If this option is used, none of this happens. This is useful with the -c option. This is equivalent to setting the variable QUIET to on.



https://www.postgresql.org/docs/9.0/app-psql.html

我还用$()替换了您的老式反引号,并将SQL查询放入参数中。

如果这样不能使其他输出静音,则可能还需要为运行cron作业的用户编辑~/.psqlrc并确保没有\timing行。

关于bash - 语法错误: invalid arithmetic operator (error token is ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53549865/

相关文章:

c - 在 Haskell 和 C 之间交换结构化数据

javascript - var functionName = function() {} vs function functionName() {}

haskell - 在 Haskell 实现中是否允许使用 Unicode 标识符是在哪里指定的?

bash - 如何在 bash 中有效地对包含 270,000 多行的文件中的两列求和

linux - 防止 case 语句不断要求输入,以便我可以执行后续命令

javascript - 带有 jquery Math 和 CSS 百分比值的视差

javascript - 如何使用用户定义的方程计算 HTML 表列(使用 javascript 或 jquery)

javascript - 使用html5 canvas在圆形中以不同 Angular 显示不同值

sql - 关于 PLSQL Exit Command 返回状态

php - 在我的测试/bootstrap/setup/etc 中使用 phpunit 命令行参数