夏期劇場
夏期劇場

Reputation: 18337

Linux Shell "Variable" to use for File Input "<"

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:

So my question here is:

Upvotes: 0

Views: 950

Answers (1)

kev
kev

Reputation: 161874

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.

Upvotes: 3

Related Questions