LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-02-2010, 03:55 AM   #1
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Rep: Reputation: 15
Thumbs up 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!
 
Old 05-02-2010, 07:12 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
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.
 
Old 05-02-2010, 07:49 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

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

Last edited by catkin; 05-02-2010 at 07:50 AM. Reason: One \ escaped
 
Old 05-03-2010, 03:23 AM   #4
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Original Poster
Rep: Reputation: 15
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.)
 
Old 05-03-2010, 03:31 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
Old 05-03-2010, 03:34 AM   #6
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

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

Last edited by Didier Spaier; 05-03-2010 at 03:37 AM.
 
Old 05-04-2010, 02:32 AM   #7
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Original Poster
Rep: Reputation: 15
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?

Last edited by Linunix; 05-04-2010 at 02:39 AM.
 
Old 05-04-2010, 02:40 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Linunix View Post
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.
 
Old 05-04-2010, 10:47 AM   #9
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Original Poster
Rep: Reputation: 15
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.)

Last edited by Linunix; 05-04-2010 at 10:54 AM.
 
Old 05-04-2010, 12:05 PM   #10
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
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"
 
Old 05-04-2010, 01:29 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Linunix View Post
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.
 
Old 05-04-2010, 07:15 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
To see exactly what bash is doing (debug mode) use this line

set -xv

as the 2nd line ie directly after the

#!/bin/bash
 
Old 05-05-2010, 03:39 AM   #13
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Original Poster
Rep: Reputation: 15
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 ..
 
Old 05-05-2010, 03:51 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Linunix View Post
(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.
 
Old 05-06-2010, 03:04 AM   #15
Linunix
Member
 
Registered: May 2005
Location: Germany
Posts: 56

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



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
batch file for accessing a shared file in ubuntu samba pdelcast Linux - Networking 5 04-24-2008 12:21 AM
batch file to convert every file in a directory with mencoder. toben Linux - Software 1 01-31-2008 05:28 PM
How to convert a batch file(.bat) to script file(.sh) manas_sem Programming 4 06-28-2007 12:10 PM
Need help with batch file Nerf Herder Linux - Newbie 6 04-22-2007 04:56 PM
writing a batch file ???? Micro Linux - General 3 01-09-2003 11:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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
Open Source Consulting | Domain Registration