为什么 xargs 不能接收参数
why xargs cannot receive argument
我有一些 sql 文件并想转储到我的本地数据库,首先我使用了这个 shell 命令,但它不起作用
ls *.sql|xargs -i mysql -uroot -p123456 foo < {}
zsh: no such file or directory: {}
但下面可以工作
echo hello | xargs -i echo {} world
hello world
那么为什么第一个命令不起作用?
在任何命令 运行 之前,重定向由 shell 处理。如果你想要 xargs
处理重定向,你需要 运行 一个 subshell.
ls *.sql | xargs -i sh -c 'mysql -uroot -p123456 foo < {}'
但是,您不应该使用 ls
来驱动脚本。你要
for f in *.sql; do
mysql -uroot -p123456 foo <"$f"
done
或者很可能只是
cat *.sql | mysql -uroot -p123456 foo
我有一些 sql 文件并想转储到我的本地数据库,首先我使用了这个 shell 命令,但它不起作用
ls *.sql|xargs -i mysql -uroot -p123456 foo < {}
zsh: no such file or directory: {}
但下面可以工作
echo hello | xargs -i echo {} world
hello world
那么为什么第一个命令不起作用?
在任何命令 运行 之前,重定向由 shell 处理。如果你想要 xargs
处理重定向,你需要 运行 一个 subshell.
ls *.sql | xargs -i sh -c 'mysql -uroot -p123456 foo < {}'
但是,您不应该使用 ls
来驱动脚本。你要
for f in *.sql; do
mysql -uroot -p123456 foo <"$f"
done
或者很可能只是
cat *.sql | mysql -uroot -p123456 foo