LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   batch to shell script conversion (https://www.linuxquestions.org/questions/linux-newbie-8/batch-to-shell-script-conversion-4175573063/)

sukhdip 02-23-2016 09:10 PM

batch to shell script conversion
 
Hi All,

I have a small tool which is currently configured in batch scripts only. But my need is to run it on Linux platform, so I have been trying to convert a batch script to shell script.
below is the batch script:

Code:

@echo off

IF "%1"== "" GOTO ARGERR


REM UPDATE THESE PROPERTIES TO MATCH YOUR BART INSTALLATION
REM -------------------------------------------------------
SET BART_HOME=D:\BARTNEW
SET BART_VER=1.5
REM -------------------------------------------------------

REM PROVIDE SYSTEM SPECIFIC INFORMATION
REM ------------------------------------
SET JRE_HOME="C:\java\jre6"
SET BROWSER_EXE="c:\program files\internet explorer\iexplore.exe"
REM ------------------------------------


REM -------------------------------------------------
REM ******* DO NOT MODIFY THE BELOW ENTRIES *********
REM -------------------------------------------------
SET JAVA="%JRE_HOME%\bin\java.exe"
SET BART_LIB_DIR=%BART_HOME%\%BART_VER%\lib
SET XALAN_LIB=%BART_LIB_DIR%\xalan.jar
SET COMMONS_LIB=%BART_LIB_DIR%\commons-collections-3.2.1.jar;%BART_LIB_DIR%\commons-configuration-1.6.jar;%BART_LIB_DIR%\commons-lang-2.4.jar;%BART_LIB_DIR%\commons-logging-1.1.1.jar;%BART_LIB_DIR%\commons-io-1.4.jar;%BART_LIB_DIR%\xmlunit-1.2.jar
SET BART_LIB=%BART_LIB_DIR%\bart.jar
SET BART_RULES_LIB=%BART_LIB_DIR%\bart-rules.jar
SET BART_CP_EXT=%XALAN_LIB%;%COMMONS_LIB%;%BART_LIB%;%BART_RULES_LIB%;
REM -------------------------------------------------
REM CALLOUT TO CUSTOM SCRIPT IF ONE EXISTS
IF EXIST %BART_LIB_DIR%\ext\setenv.bat CALL %BART_LIB_DIR%\ext\setenv.bat
REM -------------------------------------------------

IF NOT EXIST %JAVA% GOTO JAVAERR

%JAVA% -Djavax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl -Dbart.browser.path=%BROWSER_EXE% -classpath %BART_CP_EXT% com.sample.eai.tools.icg.bart.Bart %1
GOTO END

:ARGERR
ECHO Usage : launch "config-filename". Eg. launch bart.cfg
GOTO END

:JAVAERR
ECHO JRE_HOME not set correctly. Unable to locate java.exe
GOTO END

:END
set \p input="Run completed.Press Enter key to return to prompt.."

Basically this gets invoked from command line as
launch.cmd <config-file>

the above batch is saved in launch.cmd file.

Below is my effort to convert it into shell.

Code:

#!/bin/bash


# Script to convert Batch to Shell

#Set Paths for BART

export BART_HOME=/opt/BART
export BART_VER=1.5

echo "BART HOME VALUE:  $BART_HOME"
echo "BART VER VALUE: $BART_VER"

export JRE_HOME=/opt/jdk1.8.0_65/jre
echo $JRE_HOME

export JAVA="$JRE_HOME\bin\java"
export BART_LIB_DIR=$BART_HOME\$BART_VER\lib
export XALAN_LIB=$BART_LIB_DIR\xalan.jar
export COMMONS_LIB=$BART_LIB_DIR\commons-collections-3.2.1.jar;$BART_LIB_DIR\commons-configuration-1.6.jar;$BART_LIB_DIR\commons-lang-2.4.jar;$BART_LIB_DIR\commons-logging-1.1.1.jar;$BART_LIB_DIR\commons-io-1.4.jar;$BART_LIB_DIR\xmlunit-1.2.jar
export BART_LIB=$BART_LIB_DIR\bart.jar
export BART_RULES_LIB=$BART_LIB_DIR\bart-rules.jar
export BART_CP_EXT=$XALAN_LIB;$COMMONS_LIB;$BART_LIB;$BART_RULES_LIB;

$JAVA -Djavax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl -classpath $BART_CP_EXT com.sample.eai.tools.icg.bart.Bart


echo "JAVA VALUE: $JAVA"
echo "BART LIB VALUE:  $BART_LIB_DIR"
echo "XALAN_LIB VALUE: $XALAN_LIB"
echo "COMMONS_LIB VALUE: $COMMONS_LIB"
echo "BART_LIB VALUE: $BART_LIB"
echo "BART_RULES_LIB VALUE: $BART_RULES_LIB"
echo "BART_CP_EXT VALUE:  $BART_CP_EXT"


echo "#####BART RUNN STATUS#####"


This is half done as I was in the process to convert step by step. But getting the below error for jar files configurations.

Code:

./new.sh: line 20: /opt/BART$BART_VERlibcommons-configuration-1.6.jar: No such file or directory
I got stuck here only before reaching to the even last step.
Plus I'm struggling to understand and convert the below line.

Code:

:ARGERR
ECHO Usage : launch "config-filename". Eg. launch bart.cfg
GOTO END

Anybody help me with this one!!!

Thanks In Advance...

kaushalpatel1982 02-23-2016 09:57 PM

Change the
Quote:

\
with
Quote:

/
and then try.
Quote:

Generally "/" use as escape character.

Michael Uplawski 02-23-2016 11:08 PM

Quote:

Originally Posted by kaushalpatel1982 (Post 5505203)
Generally "/" use as escape character.

It is the other one "\" (backslash) that serves as escape character. But it is correct that the normal slash "/" is used for file-system objects.

But back to the script that @sukhdip presented.
GOTOS are not used in shell scripts. First of all, you should try to note these commands as part of control-structures. You could use a switch-case statement but in your script simple if-else forks will do nicely. The GOTO problem will evaporate.

Where you just need to print a message, then exit, use the exit command.

The bash has a nice man page (man bash) and if you search and concentrate just on your specific points of interest, it is not too impressive. Control structures are found in the section about “Compound Commands”.

sukhdip 02-24-2016 12:31 AM

Thanks guys!!! Silly mistake by me...didn't even get a chance to see that!!

@Michael Uplawski appreciate your effort to explain it.

chrism01 02-24-2016 03:59 AM

Also, its generally a good idea to make it explicit to the parser when you've got embedded vars ie vars in the middle of a string eg
Code:

export BART_LIB_DIR=$BART_HOME\$BART_VER\lib

# becomes
export BART_LIB_DIR=${BART_HOME}/${BART_VER}/lib

Due to the parsing rules, its not always reqd, but its a good habit to get into and makes it more obvious to other readers.

Some good links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

MadeInGermany 02-24-2016 06:25 AM

Further, a semicolom means the end of a shell command.
It needs to be \escaped or within "quotes" if part of a string.
Code:

export COMMONS_LIB="$BART_LIB_dir/commons-collections-3.2.1.jar;$BART_LIB_DIR/commons-configuration-1.6.jar;$BART_LIB_DIR/commons-lang-2.4.jar;$BART_LIB_DIR/commons-logging-1.1.1.jar;$BART_LIB_DIR/commons-io-1.4.jar;$BART_LIB_DIR/xmlunit-1.2.jar"
In general you should use quotes, and you hardly need braces.


All times are GMT -5. The time now is 06:26 PM.