[Solved] Backtick and escapes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Backtick and escapes
# 1  
Old 01-23-2013
[Solved] Backtick and escapes

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.
# 2  
Old 01-23-2013
What kind of errors do you get? Try replacing the backticks with $(...code...)
# 3  
Old 01-23-2013
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?
# 4  
Old 01-23-2013
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?
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

use backtick inside awk

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)
Discussion started by: shekhar2010us
8 Replies

2. Shell Programming and Scripting

sed with escapes AND spaces?

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)
Discussion started by: Habitual
4 Replies

3. Shell Programming and Scripting

Perl: Backtick Errors

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)
Discussion started by: kooshi
2 Replies

4. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

5. Shell Programming and Scripting

Perl: combine Backtick & system() I/O operation?

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)
Discussion started by: jeffw_00
6 Replies

6. Shell Programming and Scripting

Awk problem: How to express the backtick(')

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)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

By using awk, how to '(backtick)?

Can I know how to express the '(backtick) in awk?! By typing \' ??? (8 Replies)
Discussion started by: patrick87
8 Replies

8. UNIX for Advanced & Expert Users

Help with GNU screen: Backtick and Caption.

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)
Discussion started by: akbar
0 Replies
Login or Register to Ask a Question