使用 bash,如何从目录中的所有文件中创建类路径?
对于 bash 大师来说,这将是一个真正简单的免费赠品:
问题
使用 bash,如何从目录中的所有文件中创建类路径?
详细信息
给定一个目录:
LIB=/path/to/project/dir/lib
只包含 *.jar 文件,例如:
junit-4.8.1.jar
jurt-3.2.1.jar
log4j-1.2.16.jar
mockito-all-1.8.5.jar
我需要以以下形式创建一个以冒号分隔的类路径变量:
CLASSPATH=/path/to/project/dir/lib/junit-4.8.1.jar:/path/to/project/dir/lib/jurt-3.2.1.jar:/path/to/project/dir/lib/log4j-1.2.16.jar:/path/to/project/dir/lib/mockito-all-1.8.5.jar
一些几乎表达了我正在查找的逻辑的伪代码for 的思路是:
for( each file in directory ) {
classpath = classpath + ":" + LIB + file.name
}
通过 bash 脚本完成此操作的简单方法是什么?
This will be a really simple freebie for a bash guru:
Question
Using bash, how do you make a classpath out of all files in a directory?
Details
Given a directory:
LIB=/path/to/project/dir/lib
that contains nothing but *.jar files such as:
junit-4.8.1.jar
jurt-3.2.1.jar
log4j-1.2.16.jar
mockito-all-1.8.5.jar
I need to create a colon-separated classpath variable in the form:
CLASSPATH=/path/to/project/dir/lib/junit-4.8.1.jar:/path/to/project/dir/lib/jurt-3.2.1.jar:/path/to/project/dir/lib/log4j-1.2.16.jar:/path/to/project/dir/lib/mockito-all-1.8.5.jar
Some seudo-code that nearly expresses the logic I'm looking for would be along the lines of:
for( each file in directory ) {
classpath = classpath + ":" + LIB + file.name
}
What is a simple way to accomplish this via bash script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
新答案
(2012 年 10 月)
无需手动构建类路径列表。 Java 支持包含 jar 文件的目录的方便的通配符语法。
(请注意,
*
位于引号内部。)来自
man java
的解释:旧答案
好
简单但不完美的解决方案:
有一个小缺陷,它无法正确处理带有空格的文件名。如果这很重要,请尝试这个稍微复杂的版本:
更好
这仅在您的 find 命令支持
-printf
时才有效(就像 GNUfind
那样)。如果您没有 GNU
find
(如 Mac OS X 上那样),则可以使用xargs
代替:最好?
另一种(奇怪的)方法是更改字段分隔符变量
$IFS
。这看起来很奇怪,但对所有文件名都表现良好,并且仅使用 shell 内置命令。说明:
JARS
设置为文件名数组。IFS
更改为:
。$IFS
用作数组条目之间的分隔符。这意味着文件名之间用冒号打印。所有这些都是在子 shell 中完成的,因此对
$IFS
的更改不是永久性的(这将是 baaaad)。New Answer
(October 2012)
There's no need to manually build the classpath list. Java supports a convenient wildcard syntax for directories containing jar files.
(Notice that the
*
is inside the quotes.)Explanation from
man java
:Old Answer
Good
Simple but not perfect solution:
There's a slight flaw in that this will not handle file names with spaces correctly. If that matters try this slightly more complicated version:
Better
This only works if your find command supports
-printf
(as GNUfind
does).If you don't have GNU
find
, as on Mac OS X, you can usexargs
instead:Best?
Another (weirder) way to do it is to change the field separator variable
$IFS
. This is very strange-looking but will behave well with all file names and uses only shell built-ins.Explanation:
JARS
is set to an array of file names.IFS
is changed to:
.$IFS
is used as the separator between array entries. Meaning the file names are printed with colons between them.All of this is done in a sub-shell so the change to
$IFS
isn't permanent (which would be baaaad).这是另一种变体:
printf -v
有点像sprintf
。大括号扩展从末尾删除了多余的冒号。Here's another variation:
printf -v
is somewhat likesprintf
. The brace expansion removes the extra colon from the end.最好的
一切都比
awk
更好 =)Bestest
Everything is better with
awk
=)