[英]strange behaviour : export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo “${JARS[*]}”)
export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo "${JARS[*]}")
If I put this line in a bash_script.sh
and do 如果我将此行放在bash_script.sh
并执行
chmod +x bash_script.sh
and then run 然后运行
./bash_script.sh
it gives the error. 它给出了错误。
Syntax error: "(" unexpected (expecting ")")
How ever I am able to run this thing directly from the prompt and get the expected result. 我如何能够直接在提示符下运行此程序并获得预期的结果。 as 如
$ export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo "${JARS[*]}")
I was wondering what is the reason for this strange behaviour. 我想知道这种奇怪行为的原因是什么。
将#!/bin/bash
添加为第一行,以强制其在bash shell中运行。
Make sure you have #!/bin/bash
at the top of your shell script. 确保您的Shell脚本顶部有#!/bin/bash
。 Array syntax VAR=(...)
is a bash-ism. 数组语法VAR=(...)
是一种bash-ism。 It won't work in plain sh ( #!/bin/sh
). 它在普通sh( #!/bin/sh
)中不起作用。
By the way, that looks like a line from my answer here . 顺便说一句,这就像我在这里回答的那句话 。 If so, I encourage you to use my updated solution rather than this. 如果是这样,我鼓励您使用更新的解决方案,而不要使用此解决方案。
There's no need to manually build the classpath list. 无需手动构建类路径列表。 Java supports a convenient wildcard syntax for directories containing jar files. Java支持包含jar文件的目录的便捷通配符语法。
java -cp "$LIB/*"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.