0

I'm trying to make mysql database input from a file. But the db filename will be coming from the variable filename= /file/path/name.sql. But when input using < it is showing ambiguous redirect error.

My full script is:

filename= /var/backup/mydatabase.sql
echo $filename
mysql -uxxxx -pxxxx -hxxxx databasename < $filename

And the output is:

/var/backup/mydatabase.sql

./myscript.sh: line 3: $filename: ambiguous redirect

Please note followings:

  • Please assume the filename variable is needed for some reason
  • I'm a root user

So my question here is:

  • How to use shell variables for file input purpose?
3
  • There's really no reason to be running the mysql client as root... Commented Nov 21, 2012 at 2:42
  • Oh really! Don't do anything as root that is not absolutely necessary. Commented Nov 21, 2012 at 4:04
  • Also, please do not edit the question so as to destroy the meaningfulness of the answer. Accept the answer instead. Commented Nov 21, 2012 at 4:05

1 Answer 1

3
filename= /var/backup/mydatabase.sql
         ^

Spaces around = are not allowed when you define a variable.
Please remove the space after =.


./myscript.sh: line 3: $filename: ambiguous redirect

This error message tells your that $filename may expand to multi-words.
Please double-quote "$filename".


Make sure that script file doesn't contain any invisible ctrl-chars at end of line.
Use command xxd myscript.sh to check.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes. You are setting filename to nothing and then attempting to run /var/backup/mydatabase.sql as a command, which should produce an error message before the ambiguous redirect thing.
Then you didn't do it right. The assignment should be filename=/var/backup/mydatabase.sql. No spaces anywhere. If you do that and still get an error, then you have either typoed the variable name in one of the two lines, or your simplified version of the problem is too simplified.
NO. Please see my output (i edited). The $filename is coming. Problem is not that.
Is the filename really /var/backup/mydatabase.sql, or does it maybe have a space in it?
NO SPACE between :/ .. and not working also with this filename=/var/backup/mydatabase.sql :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.