简体   繁体   中英

Commands run in terminal but not inside a bash script

I'm new to Linux but i'm doing a tutorial for 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 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. However, when I put these commands into a bash script called build.sh:

#!/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: 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.

For some reason, antlr4 and grun are not in your $PATH.

type antlr4 

will show you where it it. Add that to your PATH in your .bashrc or .profile

antlr4 and grun are aliases that you have defined in your command window.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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