使用bash,你怎么做一个类路径的所有文件的目录 [英] Using bash, how do you make a classpath out of all files in a directory

查看:141
本文介绍了使用bash,你怎么做一个类路径的所有文件的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个真正简单的赠品为一个bash大师:

使用bash,你是怎么做出来的类路径目录中的所有文件?



详细信息

给定一个目录:

  LIB = /路径/要/项目/ 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变量:

<$p$p><$c$c>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

有些seudo- code,近前presses我正在寻找将线沿线的逻辑:

 为(以目录的每个文件){
   CLASSPATH = classpath中+:+ LIB + file.name
}

什么是一个 简单办法通过bash脚本来完成呢?


解决方案

新答案结果
(2012年10月)

有没有需要手动生成类路径列表。 Java支持包含jar文件目录的一个方便的通配符语法。

  java命令$ LIB / *

(注意: * 里面的 的引号。)

男人的java 说明:


  

作为一种特殊的方便,包含一个*基名的类路径元素被视为等同于指定的所有文件的清单目录中的扩展名为的.jar .JAR (Java程序说不出的两个调用之间的差异)。


  
  

例如,如果目录中包含富 a.jar文件 b.JAR ,那么类路径元素富/ * 扩展到 a.jar文件:b.JAR ,不同的是jar文件的顺序是不确定的。在指定的目录中的所有jar文件,甚至是隐藏的,被列入名单。由单纯的 * A类路径条目扩展到当前目录下的所有jar文件的列表。在 CLASSPATH 环境变量,其中规定,将同样扩大。 Java虚拟机启动&MDASH之前出现的任何类路径通配符扩展;没有Java程序永远不会看到未展开的通配符,除了通过查询环境。



旧答案

简单但不完美的解决方案:

  CLASSPATH = $(回声$ LIB/ *罐子| TR''':')

有在这一个略有瑕疵将不能正确处理与空格的文件名。如果该事项试试这个稍微复杂些的版本:

更好

  CLASSPATH = $(发现$ LIB-name'的* .jar-printf'%P:'| SED的/:$ //')

这只是工作,如果你find命令支持 -printf (如GNU 找到一样)。

如果你没有GNU 找到,在Mac OS X中,可以使用的xargs 而不是:

  CLASSPATH = $(找到-name'的* .jar| xargs的回声| TR'',':')

最佳?

另一个(怪异)的方式来做到这一点是改变字段分隔符变量 $ IFS 。这是很奇怪的,但在所有的文件名表现良好,而仅使用shell内建的。

  CLASSPATH = $(JARS =($ LIB/ *罐); IFS =:;回声$ {JARS [*]})

说明:


  1. JARS 设置为文件名的数组。

  2. IFS 更改为

  3. 数组是呼应,和 $ IFS 作为数组项之间的分隔符。含义的名字都印有他们之间的冒号文件。

所有这一切都在一个子shell这样做使更改 $ IFS 是不是永久性的(这将是baaaad)。

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?

解决方案

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.

java -cp "$LIB/*"

(Notice that the * is inside the quotes.)

Explanation from man java:

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to a A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be similarly expanded. Any classpath wildcard expansion occurs before the Java virtual machine is started — no Java program will ever see unexpanded wildcards except by querying the environment.


Old Answer

Good

Simple but not perfect solution:

CLASSPATH=$(echo "$LIB"/*.jar | tr ' ' ':')

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

CLASSPATH=$(find "$LIB" -name '*.jar' -printf '%p:' | sed 's/:$//')

This only works if your find command supports -printf (as GNU find does).

If you don't have GNU find, as on Mac OS X, you can use xargs instead:

CLASSPATH=$(find "." -name '*.jar' | xargs echo | tr ' ' ':')

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.

CLASSPATH=$(JARS=("$LIB"/*.jar); IFS=:; echo "${JARS[*]}")

Explanation:

  1. JARS is set to an array of file names.
  2. IFS is changed to :.
  3. The array is echoed, and $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).

这篇关于使用bash,你怎么做一个类路径的所有文件的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆