I am trying to use Bash to build a request in my postgres db. I have an asnums array and I want to query the table (dcan_nodup) in the database (ixmaps) to calculate how many rows appear in asnums.
The following code fragment is executed, which is missing below it.
WORKS (creates a comma-separated csv with the expected values):
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
DOES NOT WORK:
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 Results:
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 Results:
asnum = 6461 or asnum = 17025
ERROR: invalid input syntax for type boolean: "asnum = 6461 or asnum = 17025"
NOPE
asnum = 33139
ERROR: invalid input syntax for type boolean: "asnum = 33139"
...
What I want to do is use the OR conditions to combine the counts from two or more of asns. If I do this manually, it will work fine, for example:
ixmaps@trgen:~/scripts$ psql -d ixmaps -Atc "select count(distinct traceroute_id) from dcan_nodup where asnum = 6461 or asnum = 17025;"
124
But I can't get it to work inside the Bash loop ...
I suspect that I either do not avoid the variable correctly, and not concatenate the lines correctly or something is equally trivial - I am new to Bash ...