Solved

Add ` to Shell Script

Posted on 2010-11-29
10
460 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 277 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 112 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 112 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
 
LVL 3

Assisted Solution

by:chrios
chrios earned 277 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 56 total points
ID: 34236709
Can you use

"SELECT * FROM '$TABLENAME';"
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

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 277 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 277 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 277 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 55 total points
ID: 34251054
remove any \` and ` just leave clear table name with ; at the end and try.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now