简体   繁体   English

命令在终端中运行,但不在bash脚本中运行

[英]Commands run in terminal but not inside a bash script

I'm new to Linux but i'm doing a tutorial for Antlr4. 我是Linux的新手,但我正在为Antlr4做一个教程。 Recently, I've come into a problem. 最近,我遇到了一个问题。 When I try to set up my Antlr4 project, I run the following commands in a terminal: 当我尝试设置Antlr4项目时,我在终端中运行以下命令:

antlr4 MyGram.g4
javac *.java
grun MyGram prog test.in
dot -Tpdf test.dot > test.pdf

Everything runs fine and it creates a pdf file showing the parse tree. 一切运行正常,它创建了一个显示分析树的pdf文件。 However, when I put these commands into a bash script called build.sh: 但是,当我将这些命令放入名为build.sh的bash脚本中时:

#!/bin/bash

antlr4 MyGram.g4
javac *.java
grun MyGram prog test.in
dot -Tpdf test.dot > test.pdf

and then run the command: ./build.sh and I get the following errors and no pdf file is created: 然后运行命令:./build.sh,出现以下错误,并且没有创建pdf文件:

./build.sh: line 3: antlr4: command not found
javac: file not found: *.java
Usage: javac <options> <source files>
use -help for a list of possible options
./build.sh: line 5: grun: command not found
Error: dot: can't open test.dot

Can anyone please explain the reason why I'm getting these errors ? 谁能解释我得到这些错误的原因? I'm running Ubuntu 18.04 on VMware Workstation 14 Player. 我正在VMware Workstation 14 Player上运行Ubuntu 18.04。

For some reason, antlr4 and grun are not in your $PATH. 由于某些原因,antlr4和grun不在$ PATH中。

type antlr4 

will show you where it it. 会告诉你它在哪里。 Add that to your PATH in your .bashrc or .profile 将其添加到.bashrc或.profile中的PATH中

antlr4 and grun are aliases that you have defined in your command window. antlr4grun是您在命令窗口中定义的别名。

When you run your script, you are in a different environment. 运行脚本时,您所处的环境不同。

So you should either: 因此,您应该:

  1. define your aliases at the beginning of your script 在脚本的开头定义别名
  2. not use aliases in your script 不要在脚本中使用别名

I agree with @Tejas comment: you should use full paths in your script (far easier to maintain) 我同意@Tejas的评论:您应该在脚本中使用完整路径(易于维护)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM