Bash,where 条件数组,postgres select
Bash, array of where conditions, postgres select
我正在尝试使用 Bash 在我的 postgres 数据库上构建查询。
我有一个 asnums 数组,我想在数据库 (ixmaps) 中查询一个 table (dcan_nodup),以计算这些 asnums 出现的行数。
下面的代码片段 有效 ,下面的代码无效。
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 条件来组合 2 个或更多 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 新手...
您的问题可能是您在展开 $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 ...;"
像这样?
declare -a carriers=(6461 17025 33139 17899 7018 5730 4466 577 6549 11489)
function join { local IFS=""; 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
我正在尝试使用 Bash 在我的 postgres 数据库上构建查询。 我有一个 asnums 数组,我想在数据库 (ixmaps) 中查询一个 table (dcan_nodup),以计算这些 asnums 出现的行数。
下面的代码片段 有效 ,下面的代码无效。
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 条件来组合 2 个或更多 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 新手...
您的问题可能是您在展开 $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 ...;"
像这样?
declare -a carriers=(6461 17025 33139 17899 7018 5730 4466 577 6549 11489)
function join { local IFS=""; 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