LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with batch file (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-batch-file-805420/)

Linunix 05-02-2010 03:55 AM

Help with batch file
 
Hello
I wonder if anybody could help me out in a quick translation of a Windows batch file for the Linux environment. It is quite simple, so I guess I wouldn't need to learn all about bash or whatever.

The Windows script is (excerpt) as follows:

Code:

@echo off
setlocal
set _PORTROOT=NeuInstall\Felix
set _JAVACMD=java.exe

if exist "%_PORTROOT%\jre\bin\java.exe" set _JAVACMD="%_PORTROOT%\jre\bin\java.exe"

set _FELIXJAR="%_PORTROOT%\felix.jar"
if exist %_FELIXJAR% goto startjar

set _FELIXJAR="%_PORTROOT%\felix-2.jar"
if exist %_FELIXJAR% goto startjar
goto ende

:startjar
%_JAVACMD% -Xmx256m -jar %_FELIXJAR% %1 %2 %3 %4
goto ende

:ende
endlocal

So basically what I need to know is
- setting local variables
- dereferencing local variables
- test for file or dir existence
- passing command parameters
- if-then-else construct

It is important than "_PORTROOT" contains a relative path name (as opposed to an absolute path name).

What scripting language is to be used? In other words: what scripting language am I into normally on a Linux console prompt?

Thank your for your comment!

Didier Spaier 05-02-2010 07:12 AM

There are a lot of scripting languages usable with Linux, but by far the most common is BASH.

"man bash" will tell you everything about BASH syntax - but is a bit terse and hard to read for newcomers.

Go to this page and you'll find everything you need.

I suggest you begin with "Introduction to Linux - A Hands on Guide" and/or "Bash Guide for Beginners".

Then the "Advanced Bash-Scripting Guide" will tell you a lot more.

catkin 05-02-2010 07:49 AM

Something like (not tested and I'm not fluent in .bat programming)
Code:

#!/bin/bash

_PORTROOT=NeuInstall\Felix

_JAVACMD=java.exe
[[ -f "$_PORTROOT/jre/bin/java.exe" ]] _JAVACMD=$_PORTROOT/jre/bin/java.exe

_FELIXJAR="$_PORTROOT/felix.jar"
if [[ ! -f "$_FELIXJAR "" ]]; then
    _FELIXJAR="$_PORTROOT/felix-2.jar"
fi

$_JAVACMD -Xmx256m -jar $_FELIXJAR "$1" "$2" "$3" "$4"

Notes:
  1. Variables in a script are private to the script so there is no setlocal equivalent and it is not required.
  2. Pathname separators in Linux are / not \.
  3. The double quotes in "$1" "$2" "$3" "$4" are defensive -- in case the variables contain whitespace
.

Linunix 05-03-2010 03:23 AM

Thank you very much for the answers!
I try to work it out ..

After creating a script file I cannot run it from the console. Could you give me a hint as to how to start such a script. When typing its name I get reply "no such command ..." or similar.

Starting it from Nautilus creates a flash of terminal but immediately disappears again. Looks like a script problem, but how to assess it? (I'ld need the console window to stay open.)

EricTRA 05-03-2010 03:31 AM

Hi,

Did you make your script executable? And how do you start it? Just by typing the name?

To make executable:
Code:

chmod +x <yourscriptname>
To run:
Code:

sh <yourscriptname>
or
Code:

./<yourscriptname
The first option will run your script even if you haven't set the execution bit. The second is for when you made your script executable.

Kind regards,

Eric

Didier Spaier 05-03-2010 03:34 AM

To run the scripts you need, either to make it executable, or to launch bash and give the script name as an argument.

So do this:
chmod +x <script name>
then go to the directory where the script live (cd /full/path/to/directory) and type:
./</script name>
or:
/full/path/to/directory/<script name>

alternatively you could type:
bash <script name>
provided you are in the directory where teh script live, or:
bash /full/path/to/directory/<script name>

EDIT Just late, again :(

Linunix 05-04-2010 02:32 AM

Hi again
I'm a bit at a loss because I can't find the error.
Currently I am testing the following work-out:

Code:

#!/bin/bash

_PORTROOT=NeuInstall/FELIX-Java

_JAVACMD=java
if [ -f "$_PORTROOT/ljre/bin/java" ]; then
  _JAVACMD=./$_PORTROOT/ljre/bin/java
fi
echo JAVACMD = $_JAVACMD

_FELIXJAR="$_PORTROOT/FELIX.jar"
if [ ! -f "$_FELIXJAR" ]; then
    _FELIXJAR="$_PORTROOT/FELIX-s.jar"
fi
echo FELIXJAR = $_FELIXJAR

$_JAVACMD -Xmx256m -jar $_FELIXJAR #"$1" "$2" "$3" "$4"

I've started it with "sh felix" (where 'felix' is the batch file) and receive the following reply at the prompt. I've tried some variants but it is always the same.

Code:

: not found
felix: 20: Syntax error: end of file unexpected (expecting "then")

Any help?

catkin 05-04-2010 02:40 AM

Quote:

Originally Posted by Linunix (Post 3956503)
Hi again
I'm a bit at a loss because I can't find the error.
Currently I am testing the following work-out:

Code:

#!/bin/bash

_PORTROOT=NeuInstall/FELIX-Java

_JAVACMD=java
if [ -f "$_PORTROOT/ljre/bin/java" ]; then
  _JAVACMD=./$_PORTROOT/ljre/bin/java
fi
echo JAVACMD = $_JAVACMD

_FELIXJAR="$_PORTROOT/FELIX.jar"
if [ ! -f "$_FELIXJAR" ]; then
    _FELIXJAR="$_PORTROOT/FELIX-s.jar"
fi
echo FELIXJAR = $_FELIXJAR

$_JAVACMD -Xmx256m -jar $_FELIXJAR #"$1" "$2" "$3" "$4"

I've started it with "sh felix" (where 'felix' is the batch file) and receive the following reply at the prompt. I've tried some variants but it is always the same.

Code:

: not found
felix: 20: Syntax error: end of file unexpected (expecting "then")


Works for me, copy-and-pasted from your post. Are you creating the file in a Windows editor? If so then yo need to convert its line ends by processing it with the fromdos command.

Linunix 05-04-2010 10:47 AM

Ah, that was the problem! :) I edited the file in geEdit, but with a source coming from Windows. Obviously the crlf have been preserved all the time. With a few additions, the script is running fine now.

One problem remains. This is my current start command in the script:

Code:

"$_JAVACMD" -Xmx256m -jar "$_FELIXJAR" "$1" "$2" "$3" "$4"
Expressions '"$1"' etc. are meant to transfer commandline parameters from the batch over to the java program. I wonder if this is correctly done because I receive an unexpected parameter when leaving these values void. Strange as it might seem, this parameter is the device denotation of where program and batch reside ("/media/USBDISKPRO"). Is that possible? (Might as well be a program bug.)

Didier Spaier 05-04-2010 12:05 PM

To check you could insert these commands in your script before the quoted one:
Code:

echo '"$_JAVACMD" -Xmx256m -jar "$_FELIXJAR" "$1" "$2" "$3" "$4"'
echo "$_JAVACMD" -Xmx256m -jar "$_FELIXJAR" "$1" "$2" "$3" "$4"


catkin 05-04-2010 01:29 PM

Quote:

Originally Posted by Linunix (Post 3956976)
One problem remains. This is my current start command in the script:

Code:

"$_JAVACMD" -Xmx256m -jar "$_FELIXJAR" "$1" "$2" "$3" "$4"
Expressions '"$1"' etc. are meant to transfer commandline parameters from the batch over to the java program. I wonder if this is correctly done because I receive an unexpected parameter when leaving these values void.

My mistake -- I assumed they would always be present; when they are not an undesirable effect of the double quotes is to create empty strings as arguments. Try
Code:

"$_JAVACMD" -Xmx256m -jar "$_FELIXJAR" "$@"
The "$@" is explained here.

chrism01 05-04-2010 07:15 PM

To see exactly what bash is doing (debug mode) use this line

set -xv

as the 2nd line ie directly after the

#!/bin/bash

Linunix 05-05-2010 03:39 AM

Thanks a lot, everyone! Really great help here!! :)

(Empty Parameters)
I solved the problem by filtering out empty parameters on program level. You never know ..

catkin 05-05-2010 03:51 AM

Quote:

Originally Posted by Linunix (Post 3957809)
(Empty Parameters)
I solved the problem by filtering out empty parameters on program level. You never know ..

A robust solution :)

The Thread Tools menu can be used to mark threads SOLVED.

Linunix 05-06-2010 03:04 AM

If you allow me another question: Is it common to use certain file extentions for bash script files? I see advantage in using some to avoid possible naming conflicts with directories e.g.


All times are GMT -5. The time now is 11:18 PM.