我正在尝试使用Bash在我的postgres db上构建查询。 我有一个asnums数组,我想查询数据库中的表(dcan_nodup)(ixmaps),以计算这些asnum出现的行数。
以下代码段正常工作,其下方没有。
WORKS(使用期望值生成逗号分隔的csv):
declare -a carriers=(6461 17025 33139 17899 7018 5730 4466 577 6549 11489)
echo ""
echo "Generating CrFreqTR..."
for asnum in "${carriers[@]}"
do
echo $asnum
count=$(psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where asnum = '$asnum';")
echo $count
echo $asnum", "$count >> dcan_crfreq_tr.csv
done
不工作:
declare -a whereConditions=('asnum = 6461 or asnum = 17025' 'asnum = 33139' 'asnum = 17899' 'asnum = 7018 or asnum = 5730 or asnum = 4466' 'asnum = 577 or asnum = 6549 or asnum = 11489')
for w in "${whereConditions[@]}"
do
echo $w
echo psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where "$w";"
psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where "$w";"
echo "NOPE #1"
count=$(psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where '$w';")
echo $count
echo "NOPE #2"
echo $w", "$count >> dcan_crfreq_tr.csv
done
NOPE#1结果:
asnum = 6461 or asnum = 17025
psql -d ixmaps -Atc select count(distinct traceroute_id) from dcan_nodup where asnum = 6461 or asnum = 17025;
psql: warning: extra command-line argument "6461" ignored
psql: warning: extra command-line argument "or" ignored
psql: warning: extra command-line argument "asnum" ignored
psql: warning: extra command-line argument "=" ignored
psql: warning: extra command-line argument "17025;" ignored
psql: FATAL: Ident authentication failed for user "="
NOPE #1
asnum = 33139
psql -d ixmaps -Atc select count(distinct traceroute_id) from dcan_nodup where asnum = 33139;
psql: warning: extra command-line argument "33139;" ignored
psql: FATAL: Ident authentication failed for user "="
NOPE #1
asnum = 17899
psql -d ixmaps -Atc select count(distinct traceroute_id) from dcan_nodup where asnum = 17899;
psql: warning: extra command-line argument "17899;" ignored
psql: FATAL: Ident authentication failed for user "="
...
NOPE#2结果:
asnum = 6461 or asnum = 17025
ERROR: invalid input syntax for type boolean: "asnum = 6461 or asnum = 17025"
NOPE #2
asnum = 33139
ERROR: invalid input syntax for type boolean: "asnum = 33139"
...
我想要做的是使用OR条件来组合两个或更多个asns的计数。如果我手动执行此操作就可以了,例如:
ixmaps@trgen:~/scripts$ psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where asnum = 6461 or asnum = 17025;"
124
但我无法在bash循环中使其工作......
我怀疑我要么没有正确地转义变量,要么没有正确地连接字符串,或者同样简单的事情 - 我是一个bash新手...
答案 0 :(得分:2)
当你展开$w
时,你的问题很可能就是从双引号中删除。
假设w='one two'
。
比较printf %s\\n "foo "$w" bar"
的输出:
foo one
two bar
到printf %s\\n "foo$wbar"
:
foo one two bar
所以你不想要
psql ... "select ... "$w"...;"
而不是你想要的
psql ... "select ... $w ...;"
答案 1 :(得分:1)
declare -a carriers=(6461 17025 33139 17899 7018 5730 4466 577 6549 11489)
function join { local IFS="$1"; shift; echo "$*"; }
echo ""
echo "Generating CrFreqTR..."
q= "copy ( select asnum count(distinct traceroute_id) from dcan_nodup where asnum in (" \
`join , "${carriers[@]"` ") group by asnum ) TO STDOUT WITH CSV";
psql -d ixmaps -Atc "$q" >> dcan_crfreq_tr.csv