Linux 获取所有目录逗号分隔并将输出发送到其他脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5991732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all directories comma separated and send output to other script
提问by mjspier
We use phpDocumentator to document our php code. The php code is in different dictionaries. The script which runs the phpDocumentator looks like this:
我们使用 phpDocumentator 来记录我们的 php 代码。php 代码在不同的字典中。运行 phpDocumentator 的脚本如下所示:
./phpdoc -d dir1,dir2,dir3,dir4
Every time we add a new directory, I have to add this one to the script.
每次我们添加一个新目录时,我都必须将这个目录添加到脚本中。
I would like to do this dynamically.
我想动态地做到这一点。
ls -d ../*test*
this lists all needed directories but space separated and not comma separated.
这列出了所有需要的目录,但空格分隔而不是逗号分隔。
Question:
题:
How can I list the directories comma separated?
如何列出逗号分隔的目录?
how can I add this list as -d parameter to the phpdoc script?
如何将此列表作为 -d 参数添加到 phpdoc 脚本中?
采纳答案by Aron Rotteveel
Use ls -dm */
to generate a comma-separated list of directories. -d
will return directories only and -m
will output to a comma-separated list.
使用ls -dm */
生成一个逗号分隔的目录列表。-d
将仅返回目录-m
并将输出到逗号分隔的列表。
You could then store the output in a variable and pass it along as an argument:
然后,您可以将输出存储在变量中并将其作为参数传递:
#!/bin/bash
MY_DIRECTORY=/some/directory
FOLDERS=`ls -dm $MY_DIRECTORY/*/ | tr -d ' '`
phpdoc -d $FOLDERS
回答by Daniel B?hmer
./phpdoc -d $(ls -dm ../*test* | tr -d ' ')
回答by cdarke
No need to call an external program (assuming bash or ksh93):
无需调用外部程序(假设是 bash 或 ksh93):
var=(../*test*)
IFS=','
phpdoc -d "${var[*]}"
See @mklement0's comment below. The problem with this solution is that it will find filenames as well as directories, therefore:
请参阅下面的@mklement0 的评论。此解决方案的问题在于它会找到文件名和目录,因此:
var=(../*test*/) # trailing / ensures directories only
IFS=','
phpdoc -d "${var[*]/%/}" # remove the trailing / from the names
The solution involving expansion on all array elements is @mklement0's (except there is no need to escape the /).
涉及对所有数组元素进行扩展的解决方案是@mklement0(除非不需要转义 /)。
回答by kenorb
Here is simpler way using find
and paste
:
这是使用find
and 的更简单方法paste
:
dirs=$(find . -type d | paste -d, -s)
and the same, but using absolute paths:
同样,但使用绝对路径:
dirs=$(find "$PWD" -type d | paste -d, -s)
and version with tr
:
和版本tr
:
dirs=$(find . -type d -print0 | tr './phpdoc -d "$dirs"
' ',')
Above will scan folders recursively, unless you'll add -maxdepth 1
to list only one level deep.
以上将递归扫描文件夹,除非您将添加-maxdepth 1
到仅一层深的列表。
then run phpdoc
as:
然后运行phpdoc
为: