substring not expanding in bash
I'm using the following script the import all database dumps that reside in /root/mysql.
Code:
#!/bin/sh ERROR 1049 (42000) at line 1: Unknown database '*' The point of using ${dumpfile##*/} is, of course, to select only the file itself, and not the whole path. Any ideas how I make it expand to what it should be? The problem is that the quotes turn it into the asterix. |
probably you need to exchange # and * at the beginning of line 2.
also I recommend you to use www.shellcheck.net to check your shell scripts. |
No, that's not the issue. Actually, this was just a c/p error. And mysql wouldn't have said "unknown database '*'" if you'd have only written * in its cli. The problem is that bash is not expanding correctly and I don't know how to make it do so.
I changed the USE and SOURCE lines into this: Quote:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''*' SOURCE '*' SET foreign_key_checks = 1' at line 1 Thanks for the link. I tested the script, seemed ok except this: With regards to for dumpfile in "/root/mysql/*": Since you double quoted this, it will not word split, and the loop will only run once. |
The '*#' in line 2 of the script is definitely a bash error. It should have produced an error message but not more than that.
The error "ERROR 1049 (42000) at line 1: Unknown database '*'" refers to the result of the mysql execution. Since the variable $ddl is one line holding the command, the error messages refers to ddl. It is not a bash error, it is a mysql error. I never used -e to execute a mysql command, but always Code:
echo $ddl | myqsl Code:
mysql -u user -ppassword database Some more hints to easy debugging:
jlinkels |
Now I see your post #3.
You have to echo the $ddl before you can make any assumption why there is an error in your command. Copy/paste the echoed string in a mysql shell, see where it goes wrong, correct and debug there. jlinkels |
I do not really like it. You post a script which is not the one you use, just something similar. Never mind.
You ought to insert set -xv at the beginning of your script and you will see what's happening. |
Quote:
Remove the quotes and try your script again. Code:
for dumpfile in /root/mysql/* |
As an aside (tho I think that michaelk has identified the root cause)...
Is there some reason for the (to me) clumsy population of ddl? Why not Code:
ddl="set names utf8; \ ...the Code:
ddl="$ddl plus something else" |
@pan64 Sorry, it was my mistake, the rest of the script is identical, I assure you (with the exception of the changes that I said I made).
@michaelk first I replaced them with single quotes, which was just as bad :) Now I removed the double quotes from /root/mysql/* altogether. Anyway, after adding the simple quotes like this Code:
ddl="$ddl USE '${dumpfile##*/}'"; @scasey. There's no specific logic to the script. I copied it from somewhere and it worked just fine as it was for a single database. The problem is that I have lots of databases and I wanted it automated. so I created the for loop, which created these problems. I added an echo line in the script like this: Code:
ddl="$ddl USE '${dumpfile##*/}'"; Code:
Import started: OK |
Your 'This is ddl variable' line in your post above has been truncated. We need to see the whole variable value to check its validity.
|
According to my read of the USE syntax, the database name should not be quoted:
Code:
USE db_name Try removing the single quotes from this Code:
ddl="$ddl USE '${dumpfile##*/}'"; |
Code:
ddl="$ddl USE '${dumpfile##*/}'";[/quote' |
This is how it looks right now:
Code:
#!/bin/sh Now it seems to be working as expected. Thanks all for your patience :) I do have another problem now, though, for some of the databases: "Got error 168 from storage engine" I've looked it up on the internet, and lots of people mentioned lack of disk space, which clearly isn't the issue in my case. Could it be related to the buffer length or max packet size variables in relation to the ram available, which is only 2GB? I'm guessing I'd have to open a new thread for this subject :) |
You're most welcome. It's important to understand that when any compiler or interpreter reports syntax errors that one should look for typos or mis-placed characters first. Mysql is very clear about where the error starts (at the beginning of the quoted chunk of code), but very vague about what the error is.
...and I don't know about "have to" open a new thread, but it would be the better way to go, IMO Please mark the thread as [SOLVED] |
I would suggest you to use different syntax, which will make it more readable:
Code:
for dumpfile in /root/mysql/* |
All times are GMT -5. The time now is 03:16 PM. |