列出bash参数中的所有目录

  • 本文关键字:bash 参数 列出 bash
  • 更新时间 :
  • 英文 :


假设我有一个bash脚本example.sh。运行该脚本需要一个参数,即目录列表。脚本的工作方式如下:

./example.sh "/dir/location/281/rest-of-the-location"  "/dir/location/287/rest-of-the-location"  "/dir/location/37/rest-of-the-location"  "/dir/location/3007/rest-of-the-location"

所有目录列表都保存在一个名为file.txt的文件中。有没有一种简单的方法可以运行脚本,而不是像上面那样将目录放在第二个引号中?

所有目录都保存在一个名为file.txt的文件中

file.txt
/dir/location/281/rest-of-the-location
/dir/location/37/rest-of-the-location
/dir/location/3007/rest-of-the-location
/dir/location/3127/rest-of-the-location
/dir/location/37/rest-of-the-location
/dir/location/372/rest-of-the-location
/dir/location/137/rest-of-the-location

使用xargs:

xargs ./example.sh < file.txt

如果example.sh与用户交互,您可能需要将-o选项添加到xargs,或者与GNU xargs一起使用xargs -a file.txt ./example.sh

最新更新