shell - 帮助使用 xargs 将多个文件名传递给 shell 脚本

标签 shell unix

有人可以告诉我如何正确使用 xargs 吗?或者如果不是 xargs,我应该使用什么 unix 命令?

我基本上想为输入<localfile>输入超过(1)个文件名, 第三个输入参数。

例如:

1. use `find` to get list of files
2. use each filename as input to shell script

shell脚本的使用:
test.sh <localdir> <localfile> <projectname>

我的尝试,但不起作用:
find /share1/test -name '*.dat' | xargs ./test.sh /staging/data/project/ '{}' projectZ \;

编辑:
经过大家的一些输入和尝试 -exec , 我发现我的 <localfile>文件名输入 find也给了我完整的路径。 /path/filename.dat而不是 filename.dat .有没有办法从 find 获取基本名称? ?我认为这必须是一个单独的问题。

最佳答案

我只是用 find -exec 这里:

% find /share1/test -name '*.dat' -exec ./test.sh /staging/data/project/ {} projectZ \;

这将调用 ./test.sh用你的三个参数每个 .dat文件下 /share1/test .

xargs 将打包所有这些文件名并将它们传递到 ./test.sh 的一次调用中,这看起来不像您想要的行为。

关于shell - 帮助使用 xargs 将多个文件名传递给 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7249814/

相关文章:

bash - 如果 ( ) </开发/空

arrays - 如何在 Bash 中对数组进行排序

PHP - 执行 bash .sh 文件以从不同服务器删除文件

linux - '-' 运算符在 Linux 中实际上做了什么?

bash - 如果Docker脚本上的哈希值不正确,如何使用sha256sum --check选项失败

php - 防止程序执行其他程序

c - 传递给线程(unix)的结构有错误的值

linux - 如何在给定 pid 的远程机器上恢复停止的作业?

c - Unix(在 C 中)尝试为 XV6 编写 tail

c - 在 Unix 上使用 C 从文件描述符读取/写入 N 个字节