cumanacr cumanacr - 1 year ago 145
Bash Question

How can I get rid of bash: $1: ambiguous redirect on xargs command

I'm trying to execute several files with sql setences on a given database:

ls -lah | grep "sql$" | awk '{print $9}' | xargs mysql -uanuser -papassword a_database < $1

But I getting the error:

bash: $1: ambiguous redirect

If I change the command on xargs to a simple
echo $1
it works

I already tried double quotes like this:
xargs mysql -uanuser -papassword a_database < "$1"
and this
"xargs mysql -uanuser -papassword a_database < $1"
with no luck the terminal bring another error:

xargs: mysql -uanuser -papassword a_database < {}: No such file or directory

Can you help me please?

Answer Source

You don't need to use ls -l | grep | awk, hust use a simple for loop on *.sql files:

for f in *.sql; do
   mysql -uanuser -papassword a_database < "$f"