LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2016, 10:10 PM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Rep: Reputation: Disabled
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...

Last edited by sukhdip; 02-23-2016 at 10:17 PM.
 
Old 02-23-2016, 10:57 PM   #2
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, PCLinuxOS
Posts: 153

Rep: Reputation: 9
Change the
Quote:
\
with
Quote:
/
and then try.
Quote:
Generally "/" use as escape character.

Last edited by kaushalpatel1982; 02-23-2016 at 10:59 PM.
 
Old 02-24-2016, 12:08 AM   #3
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian stretch/sid
Posts: 398
Blog Entries: 14

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by kaushalpatel1982 View Post
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”.

Last edited by Michael Uplawski; 02-24-2016 at 01:37 AM.
 
Old 02-24-2016, 01:31 AM   #4
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Thanks guys!!! Silly mistake by me...didn't even get a chance to see that!!

@Michael Uplawski appreciate your effort to explain it.
 
Old 02-24-2016, 04:59 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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/
 
Old 02-24-2016, 07:25 AM   #6
MadeInGermany
Member
 
Registered: Dec 2011
Posts: 193

Rep: Reputation: 73
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Batch Script to Shell Script sandeep.gcet@gmail.com Linux - Newbie 10 10-17-2013 12:36 PM
Batch file conversion to shell script aullah Linux - General 2 08-16-2011 04:00 AM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 01:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 02:49 AM
Help w/ batch/shell script mikehlinuxquest Linux - General 5 09-13-2004 05:41 PM


All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration