Solved

Add ` to Shell Script

Posted on 2010-11-29
10
467 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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 …
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
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…
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.

724 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