0

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.

  • It is always better to give full path of command in bash scripts if you don't want to fiddle with environment variables and profiles. – Tejas Sarade Jul 01 '18 at 09:59
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 01 '18 at 10:12

2 Answers2

1

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

ctrl-d
  • 392
  • 1
  • 8
1

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)

YaFred
  • 9,698
  • 3
  • 28
  • 40