Solved

Add ` to Shell Script

Posted on 2010-11-29
10
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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