LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-30-2011, 07:28 PM   #16
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943

(Check the commands in your previous post, please. I think you'll want to edit them.)
Quote:
Originally Posted by cnmoore View Post
Code:
       STR="/bin/zcat '/home/mike/backups/$1' | /usr/bin/mysql -u username -p password '$2'"
       echo -e "$STR"
       eval "$STR"
Yes, that is what I meant. The issue is that now the mysql command part,
Quote:
Originally Posted by cnmoore View Post
[CODE]
/usr/bin/mysql -u username -p password 'table'
fails. Have you made sure that the command
Code:
/bin/zcat '/home/mike/backups/mike_forum_110321-0100.sql.gz' | /usr/bin/mysql -u whoever -p xxxxxx 'test'
actually works?
 
Old 03-30-2011, 08:16 PM   #17
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Good question!

This works on command line:
Code:
/bin/gunzip <  /home/mike/backups/mike_forum_110321-0100.sql.gz |  /usr/bin/mysql -uGeorge -pxxxxx test
None of the variations introduced above work. Glad you asked!
Single quotes were in response to http://www.linuxquestions.org/questi...4/#post4308900

Last edited by cnmoore; 03-30-2011 at 08:19 PM.
 
Old 03-30-2011, 09:08 PM   #18
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
My son pointed out that even though echo -e "$STR" looks fine, $STR does not necessarily look good to the command processor. He suggested this:
Code:
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -umike_forum -psKeYzY4g $2"
        echo -e "$STR" > temp.sh
        chmod 777 temp.sh
        bash temp.sh
else
        exit
fi
and it works! Hooray!

Thanks to all of you for taking an interest.
 
Old 03-31-2011, 02:27 PM   #19
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Thumbs up Surprising fact

I got to thinking more about this. My original script didn't work, but this does work:
Code:
STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -uUser -pxxxx
eval $STR
Output with -x:
+ STR='/bin/gunzip < /home/mike/backups/mike_forum_110322-0100.sql.gz | /usr/bin/mysql uUser -pxxxx test'
+ eval /bin/gunzip '<' /home/mike/backups/mike_forum_110322-0100.sql.gz '|' /usr/bin/mysql uUser -pxxxx test
++ /bin/gunzip
++ /usr/bin/mysql uUser -pxxxx test

The 'eval' suggestion from Nominal Animal was right on, but when I tried it out it got tangled with a 'zcat' (which didn't work).

Thanks and apologies, Normal Animal.
 
Old 03-31-2011, 04:26 PM   #20
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Nominal Animal View Post
You have a redirection and a pipe there,
so you actually need to do
Code:
eval "$STR"
Being new to scripting I was curious why this is. So I did some googling and found this nice clear explanation that variable expansion occurs after output redirecton:
http://linuxdevcenter.com/pub/a/linux/lpt/08_10.html
Quote:
Do you see what's happening? We're constructing a command that will look something like:

grep $grepopts $searchstring $file | sort $sortopts > $ofile
But the entire command is "hidden" in shell variables, including the I/O redirectors and various options. If the eval isn't there, this command will blow up in all sorts of bizarre ways. You'll see messages like | not found, because variable expansion occurs after output redirection. The "nested" variables (like $ofile, which is used inside of $output) won't be expanded either, so you'll also see $ofile not found. Putting an eval in front of the command forces the shell to process the line again, guaranteeing that the variables will be expanded properly and that I/O redirection will take place.
 
Old 03-31-2011, 04:27 PM   #21
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,411
Blog Entries: 1

Rep: Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115
You are also doing in 3 lines, what can be done in 1.
 
Old 03-31-2011, 04:29 PM   #22
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
You are also doing in 3 lines, what can be done in 1.
Please explain and suggest?
 
Old 03-31-2011, 04:33 PM   #23
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,411
Blog Entries: 1

Rep: Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115Reputation: 1115
As i said before, when I re-wrote the part that was breaking...

Code:
STR="/bin/zcat '/home/mike/backups/$1' | /usr/bin/mysql -u username -p password '$2'"
echo -e "$STR"
eval "$STR"
can be done with:

Code:
/bin/zcat $1 | /usr/bin/mysql -u MyUser -pppppp $2
But thats your call. I just prefer to have less script to look at. Less is more.

Good luck and Regards.

Last edited by szboardstretcher; 03-31-2011 at 04:34 PM.
 
1 members found this post helpful.
Old 03-31-2011, 04:55 PM   #24
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
As i said before, when I re-wrote the part that was breaking...

Code:
STR="/bin/zcat '/home/mike/backups/$1' | /usr/bin/mysql -u username -p password '$2'"
echo -e "$STR"
eval "$STR"
can be done with:

Code:
/bin/zcat $1 | /usr/bin/mysql -u MyUser -pppppp $2
But thats your call. I just prefer to have less script to look at. Less is more.

Good luck and Regards.
You're absolutely right. That does work and is certainly more compact and also more transparent - which I certainly think is a good thing in a script. I didn't know about zcat before - pipe built in is cool.

I got incompatible suggestion mixed up together and didn't absorb your very helpful http://www.linuxquestions.org/questi...4/#post4308837

Thanks!

Last edited by cnmoore; 03-31-2011 at 04:57 PM.
 
  


Reply

Tags
mysql, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 08:54 AM
[SOLVED] "!#/bin/bash: No such file or directory"? When running a script C_Blade Linux - Newbie 12 04-08-2010 11:15 PM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 05:37 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM


All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration