Hello all,
I have a problem with a bash script. It contains an MySQL query, which when I run it 'as is', executes without a problem.
When, however, I try to get it to assign its output to a variable, using the backtick, I get errors.
So ..
Code:
/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";'
.. no problem.
When, however ...
Code:
result=`/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";'`
.. errors.
So I'm guessing that something in there needs to be escaped, but I can't say what.
Can someone help?
Thanks.
Last edited by radoulov; 01-23-2013 at 11:14 AM..
Reason: Marked as solved.
result=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
result=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
Does this help?
It does, yes.
I am in your debt, Sir. Thank you!
---------- Post updated at 11:52 AM ---------- Previous update was at 10:05 AM ----------
Quote:
Originally Posted by jim mcnamara
Code:
result=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
Does this help?
Can I pick your brains a bit more?
OK, so I got that script to run, and it returns '0' - which is what it should return when everything's OK.
If it returns a non-zero result, I have to run the same SQL request, only ... well, this (note that the variable names are slightly different!) ..
Code:
orderStatus=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT ordernumber FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
If there is one result to that query, then that's fine. But what if there are several? I tried an array in a bash shell - which is something I've never done, so a lot of googling was required, and came up with .. OK, here is the entire script..
Code:
## get date
today=`date +"%d-%m-%Y-%H-%S"`
if [ -f /root/extrac.txt ]; then
/bin/rm /root/extrac.txt
fi
#### check orders
result=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT COUNT(*)
FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
/bin/echo "" > /root/extrac.txt
/bin/echo "The date is $today" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "We have verified the number of orders still in 'submitted' state" >> /root/extrac.txt
/bin/echo "after four hours." >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
if [ $orderStatus != "0" ]; then
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "The number of orders in this state is not zero." >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "As such, the web order numbers are echoed below : " >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
orderStatus=$(/usr/bin/mysql -N -B mydatabase -e 'SELECT ordernumber FROM orders as o JOIN users as u ON o.userthing=u.package
WHERE o.table BETWEEN (now() - INTERVAL 1 day) AND
now() - INTERVAL 25 hour) AND o.p_stateorder=1 AND
u.name != "The Site" AND u.p_username NOT LIKE "myAddress@mailserver.com";')
while [ $result ]
do
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "$result" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
done
exit 0
fi
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "The number of orders is zero, and as such, no web order numbers are available" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "Any questions, please e-mail me@mymailaddress.com" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
/bin/echo "-------------------------------------------------------------" >> /root/extrac.txt
/bin/echo "" >> /root/extrac.txt
And then I cat the file and pipe it to mail.
For the moment, that always returns zero, but I'm not entirely sure that the day '$result' is non-zero, that I'm going to get an array.
I could probably do this in PhP, but it's not on the server, and it's a production box, so I can't install it.
Hello,
Can't we use backtick operator inside awk.
nawk '
BEGIN {
RS=""; FS="\</input\>"
}
{
for(i=1;i<=NF;i++) {
if ($i~/\"\"/) {
print `grep XYZ $i`;
print $i
}
}
}
' test
In the following code, I need to print $i and some... (8 Replies)
It's been covered in lots of places, and I have banged on it for about an hour and I am not making any headway on this particular string.
A weather-related web page I want to monitor has text on the screen, but it changes. Right now, the page has "None issued by this office recently." but in 2... (4 Replies)
When trying to use backticks for system commands, is there a way to read the error messages if a command doesn't execute properly?
I have no problem getting the results if the command is properly executed.
Ex.
my @result = `dir`;
foreach my $line (@result) {
print "Result = $line";... (2 Replies)
Hi - Within perl I want to execute a system command. I want to re-direct all the output from the command to a file (@result = `$cmd`;), but I ALSO want the results to be displayed on the screen (system("$cmd");
The reason is this - if the command completes, I want to process the output. If the... (6 Replies)
For example:
I got a list of file end at .txt. I want all of them do the same command like
grep '^@' and attached it to a output .sh file.
This is the command I type:
ls *.txt | awk '{print "grep \' \^\@\' ",$1}' > txt.sh
My desired output is when I type the command "more txt.sh "
The... (4 Replies)
I'm trying to get GNU screen to show the output of "uptime" for the host being accessed in the current window, but unfortunately, no matter what window I go in, it shows the uptime for the host I originally launched screen in ("adminhost").
Does anyone know how to get this to update from the... (0 Replies)