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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
03-30-2011, 06:28 PM
|
#16
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
(Check the commands in your previous post, please. I think you'll want to edit them.)
Quote:
Originally Posted by cnmoore
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
[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?
|
|
|
03-30-2011, 07:16 PM
|
#17
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
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 07:19 PM.
|
|
|
03-30-2011, 08:08 PM
|
#18
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
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. 
|
|
|
03-31-2011, 01:27 PM
|
#19
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
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.
|
|
|
03-31-2011, 03:26 PM
|
#20
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
Quote:
Originally Posted by Nominal Animal
You have a redirection and a pipe there,
so you actually need to do
|
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.
|
|
|
|
03-31-2011, 03:27 PM
|
#21
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
You are also doing in 3 lines, what can be done in 1.
|
|
|
03-31-2011, 03:29 PM
|
#22
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
Quote:
Originally Posted by szboardstretcher
You are also doing in 3 lines, what can be done in 1.
|
Please explain and suggest?
|
|
|
03-31-2011, 03:33 PM
|
#23
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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 03:34 PM.
|
|
1 members found this post helpful.
|
03-31-2011, 03:55 PM
|
#24
|
Member
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89
Original Poster
Rep:
|
Quote:
Originally Posted by szboardstretcher
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 03:57 PM.
|
|
|
All times are GMT -5. The time now is 11:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|