?
Solved

Add ` to Shell Script

Posted on 2010-11-29
10
Medium Priority
?
473 Views
Last Modified: 2013-12-16
The following code keeps encountering an error because of the table name, which happens to be 2010-11-28. In SQL, i can fix this by adding ` around the table name. However, I can;t seem to figure out how to do it in a shell script. Adding ` around the table name variable nullifies it. How can I add this?
#!/bin/bash

USERNAME='testuser'
PASSWORD='testuser'
TABLENAME=$(date --date=tomorrow +"%Y-%m-%d")

for a in $(cat servers.txt); do 

	for b in 1 2 3
	do

		for c in $(cat databases.txt); do 

			mysql -h $a -u$USERNAME -p$PASSWORD --socket=/var/lib/mysql/mysqld$b/mysql.sock $c -e "SELECT * FROM $TABLENAME;"

		done

	done

done

Open in new window

0
Comment
Question by:plecostomus
10 Comments
 
LVL 3

Accepted Solution

by:
chrios earned 1108 total points
ID: 34236687
I think the problem may come from the semicolon in the end. Change it to \; and try it again, like this:
mysql -h $a -u$USERNAME -p$PASSWORD --socket=/var/lib/mysql/mysqld$b/mysql.sock $c -e "SELECT * FROM $TABLENAME\;"

Open in new window

0
 
LVL 13

Assisted Solution

by:themrrobert
themrrobert earned 448 total points
ID: 34236690
you need to replace the -'s with \-  i am looking for something to help you.

0
 
LVL 13

Assisted Solution

by:themrrobert
themrrobert earned 448 total points
ID: 34236697
acually chrios's answer appears to be correct, i wasnt focused on that part. goodluck to you, reply if you still need my assistance
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 3

Assisted Solution

by:chrios
chrios earned 1108 total points
ID: 34236700
Oh, and sometimes it is better to use the variable form ${myvar} instead of $myvar. That way you can use ${myvar}abc and bash will not get confused when looking for $myvarabc.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 224 total points
ID: 34236709
Can you use

"SELECT * FROM '$TABLENAME';"
0
 

Author Comment

by:plecostomus
ID: 34236722
The semi colon isn't an issue. The problem is the table name. The select statement looks like this:

SELECT * FROM 2010-11-28;

In mysql, this fails. However, if you add "`" to it, it works. Thus, I am trying to create this:

"SELECT * FROM `${TABLENAME}`;"

However. the "`" nullifies the variable. I tried this, "SELECT * FROM \`${TABLENAME}\`;", but it doesn't seem to work either.
0
 
LVL 3

Assisted Solution

by:chrios
chrios earned 1108 total points
ID: 34236736
If he needs to use ` he must escape them first, or bash will interpret them.

 \`${TABLENAME}\` should work. I guess the complete line would look something like this:
mysql -h $a -u$USERNAME -p$PASSWORD --socket=/var/lib/mysql/mysqld$b/mysql.sock $c -e "SELECT * FROM  \`${TABLENAME}\`\;"

Open in new window

0
 
LVL 3

Assisted Solution

by:chrios
chrios earned 1108 total points
ID: 34236739
I see, try escaping the " as well with \" and see if that works.
0
 
LVL 3

Assisted Solution

by:chrios
chrios earned 1108 total points
ID: 34236772
Sorry for the guesswork so far. I tried running some tests in my own terminal and perhaps this will help you more.

It seems like escaping " * ; and ' should make it work. This code:

for TABLENAME in foo bar "baz asd"; do echo \"SELECT \* FROM \`$TABLENAME\`\;\"; done

Open in new window


generates the following output:
"SELECT * FROM `foo`;"
"SELECT * FROM `bar`;"
"SELECT * FROM `baz asd`;"

Open in new window


Is that closer to what you need?
0
 
LVL 3

Assisted Solution

by:ivailoj
ivailoj earned 220 total points
ID: 34251054
remove any \` and ` just leave clear table name with ; at the end and try.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question