How to make a subquery to a database using SED?

The task is simple you need to bring in a new line the name of the domain from the subquery to the database via the ID in the mail
Watching the mail queue
mailq|grep ^[A-F0-9] |cut-c 42-80| sort | uniq -c| sort-n


2 web175@ks02.ru
5 web174@ks02.ru
6 web137@ks02.ru
8 web221@ks02.ru
10 web217@ks02.ru
27 web178@ks02.ru


Let us leave for convenience only ID sites
mailq|grep ^[A-F0-9] |cut-c 42-80| sed -e s'/.*web\([0-9]\{1,\}\)\(@ks03.ru\)$/\1 /g


217
217
137
221
178
178
178
217
137
221
178
178
174
217
178
178
137
178
178
178
178
178

throw in an array
LIST=$(mailq|grep ^[A-F0-9] |cut-c 42-80| sed -e s'/.*web\([0-9]\{1,\}\)\(@ks02.ru\)$/\1 /'g)
echo $LIST

for ELEMENT in $LIST
do
$ELEMENT"
 mysql-uroot -p********* -N-B dbispconfig -e 'select domain from 'web_domain' where 'domain_id'=$ELEMENT;'

 done

And then if you print to make it all work, but if you just accomplish something the sequel fixed.
Don't know which quotes need to remove and what to escape.
April 4th 20 at 13:03
2 answers
April 4th 20 at 13:05
'web_domain', 'domain_id' in the context of the query - string constants
`web_domain` - table
`domain_id` field

'=> `
April 4th 20 at 13:07
'select domain from 'web_domain' where 'domain_id'=$ELEMENT;'


So single quotes when you put all, you do not bother?
Try this
"select domain from 'web_domain' where 'domain_id'=$ELEMENT;"
no, do not bother even if they are all clean there is no difference ;)
and the option of shielding too - Natalia_Shields commented on April 4th 20 at 13:10
LIST=$(mailq|grep ^[A-F0-9] |cut-c 42-80| sed -e s'/.*web\([0-9]\{1,\}\)\(@ks02.ru\)$/\1 /'g)
echo $LIST

for ELEMENT in $LIST; do
 echo "processing [$ELEMENT]"
 mysql-uroot -p********* -N-B dbispconfig -e "select domain from 'web_domain' where 'domain_id'=$ELEMENT;"
done


The result here is what displays? - Benni commented on April 4th 20 at 13:13
@Benni,

processing [web221@ks02.ru]
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "web_domain' where 'domain_id'=web221@ks02.ru' at line 1
processing [root@ks02.ru]
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server vers



Yesterday I cast on this version. he is a worker. but it looks clumsy to put it mildly ;)
#!/bin/bash
LIST=$(mailq|grep ^[A-F0-9] |cut-c 42-80| sed -e s'/.*web\([0-9]\{1,\}\)\(@ks02.ru\)$/\1 /'g)
#echo $LIST
rm-r 11.txt
for ELEMENT in $LIST
do
#echo-n $ELEMENT \s
echo mysql-uroot -p******* -N-B dbispconfig -e \'select domain from 'web_domain' where 'domain_id'=$ELEMENT\;\' | sh >> 11.txt
done
cat 11.txt | sort | uniq -c| sort-n
- Natalia_Shields commented on April 4th 20 at 13:16
So it probably should be in quotes and the value to take, since there is a line

LIST=$(mailq|grep ^[A-F0-9] |cut-c 42-80| sed -e s'/.*web\([0-9]\{1,\}\)\(@ks02.ru\)$/\1 /'g)
echo $LIST

for ELEMENT in $LIST; do
 echo "processing [$ELEMENT]"
 mysql-uroot -p********* -N-B dbispconfig -e "select domain from 'web_domain' where 'domain_id'='$ELEMENT';"
done
- Benni commented on April 4th 20 at 13:19
@Benni,
echo works and the element is a variable
but if you run the command then he or variable then the sequel doesn't see the confused or variable or in quotes.
if you see a working example, I once again echo and generate a string and then give it to sh
if you do the same thing but sh $(everything) that doesn't work
but echo (everything) | sh is already working - Natalia_Shields commented on April 4th 20 at 13:22
I think we need more logs and better understand what your items look like string.
For example of your error:

right syntax to use near "web_domain' where


Why web_domain left two quotation marks, and the right one?
You have the values quotes to meet?
Just add more debugging echo. If your Element and other variables meet special characters that can be processed by bash, then they must be escaped. - Benni commented on April 4th 20 at 13:25
@Benni, when you remove the quotes at all in the SQL query, nothing changes. - Natalia_Shields commented on April 4th 20 at 13:28
I recommend - do more debugging echo
make

MYCMD="mysql -uroot -p********* -N-B dbispconfig -e \"select domain from 'web_domain' where 'domain_id'='$ELEMENT';\""
echo "$MYCMD"
$(MYCMD)

look at what the command is run.
Try to run a query in mysql without cycle,manually type the whole thing for a specific element. - Benni commented on April 4th 20 at 13:31

Find more questions by tags MySQLbashsed