?
Solved

Add ` to Shell Script

Posted on 2010-11-29
10
Medium Priority
?
469 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 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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