[英]“No such file or directory” when running java from shell script
I'm trying to run a script from an Amazon Linux machine. 我正在尝试从Amazon Linux计算机运行脚本。 The script invokes checkstyle like this (in a script called eval.sh):
该脚本像这样调用checkstyle(在名为eval.sh的脚本中):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err: 运行此命令时,在quality.err中收到以下错误:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. 我试图直接在终端中运行相同的命令,它正在工作。 Both checkstyle-6.15-all.jar and checks.xml are where they should be.
checkstyle-6.15-all.jar和checks.xml都在应有的位置。
What could cause this problem? 是什么导致此问题?
When you envoke "${CHECKSTYLE}"
the shell thinks that is the command you are running. 当您调用
"${CHECKSTYLE}"
,shell会认为这是您正在运行的命令。 There is no such file name with the spaces and options have you have included there. 没有这样的文件名,其中包含空格和选项。 If you envoke it simply as
${CHECKSTYLE}
(drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process. 如果您以
${CHECKSTYLE}
简单地调用它(删除引号),则外壳程序将像${CHECKSTYLE}
空白一样正常处理它,并将其拆分为适当的部分以创建该过程。
Change "${CHECKSTYLE}"
to ${CHECKSTYLE}
(without the quotes). 将
"${CHECKSTYLE}"
更改为${CHECKSTYLE}
(不带引号)。
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar
, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar
. 您正在将CHECKSTYLE变量的整个值作为一个单词传递(这就是引号所做的事情),因此外壳程序正在寻找一个名为
java -jar
的相对目录,并尝试在该目录(不存在)下使用以下命令查找文件:路径home/ec2-user/grader/ext/checkstyle-6.15-all.jar
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.