• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Converting MSDOS batch file to Linux shell script

Hi all,

I'm new to Linux, and I've got one little problem.

I have a batch file in Windows that I wrote to compile and run some Java files. However, I also need to do that in Linux. I know that it is called shell script, but I know nothing about it.

Here is the batch file:

@echo off
if exist "%JAVA_HOME%\bin\javac.exe" goto okExecute
if exist "%PATH%\javac.exe" goto okExecute
if not exist "%JAVA_HOME%\bin\javac.exe" goto notExecute
if not exist "%PATH%\javac.exe" goto notExecute

echo JAVA_HOME environment variable not found
goto end

javac -sourcepath src -classpath classes src\compiler\Lex.java -d classes
java -classpath classes compiler.Lex %1 %2
goto end



I would really appreciate if you help me!

Thanks in advance.
  • 2
1 Solution
shell scripts usually end in the .sh extension, they are simply a text file full of commands you'd type in the command prompt.

EG: If you wanted to compile 2 java files in your home directory, this may be what you script looks like:

javac /home/myname/javafile1.java
javac /home/myname/javafile2.java

Then, you'd save this file maybe as /home/myname/compile.sh

To run it you would then type in the terminal/prompt:
# ./home/myname/compile.sh

If you get a permission error type:
# chmod 777 /home/myname/compile.sh

You can also set env variables jus tlike windows for JAVA_HOME etc... but before I ramble on just read:

Shell equiv (more or less) is:

if [ -z "$JAVA_HOME" ]
  echo "JAVA_HOME environment variable not set"
  exit 1

$JAVA_HOME/bin/javac -sourcepath src -classpath classes src/compiler/Lex.java -d classes
$JAVA_HOME/bin/java -classpath classes compiler.Lex $1 $2

brunoguimaraesAuthor Commented:
Sorry I took so long to answer back.

Thanks for your help Tintin. I accidentally gave you a grade B. How can I change it to A?
You can post a request in the "Community Support" forum for any changes.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now