Writing Bash shell scripts corresponding to windows bat files

Experts,

I am newbie in shell scripting. I want to write Bash shell scripts corresponding to windows bat files. I have installed cygwin at c:\cygwin and i am trying to crate the sh file using vi editor. i am not able to understand how to use linux/unix convention for the code. following is my code.

@ECHO OFF
IF NOT DEFINED MY_HOME goto home_not_set
IF DEFINED JAVA_HOME GOTO java_defined
ECHO Setting Java Home
set JAVA_HOME="%MY_HOME%\java\jre6_14"
SET PATH=%JAVA_HOME%\bin;%PATH%
:java_defined
set ACTIVEMQ_HOME=%MY_HOME%\message
GOTO end
:home_not_set
ECHO Set environment variable MY_HOME before running this script
GOTO end
:end
ENDLOCAL

where can i find a document or sample code to use in cygwin? i tried with #!/bin/bash. it started to print error at the end of file. do i need to set any path before writing scripts? please suggest or advise.

Thank you.

Here are the answers:

The code in shell:

# man test
if [ -z "${MY_HOME}" ]
then
	echo "Set environment variable MY_HOME before running this script".
	exit 1
fi

# man test
if [ -z "${JAVA_HOME}" ]
then
	export JAVA_HOME="${MY_HOME}\java\jre6_14"
	export PATH="${JAVA_HOME}\bin;${PATH}"
fi

export ACTIVEMQ_HOME="${MY_HOME}\message"

About the document, you can find something in CygWin's web site.

The error you are facing can be due to some CR's in the shell script file. Run the command dos2unix on it.

dos2unix <YourScript>
1 Like

@felipe.vinturin

Thank you very much for your reply friend. Thank you. Where can i find the corresponding shell script where i can learn the syntax in detail? It will be a great help as i have plenty files convert from .bat to .sh. Thank you in advance.

A link I post another day in another post is this one: Unix shell scripting with ksh/bash

Also, if you have doubts you can post your questions, we have excelent people here!

Regards.

1 Like

Batch files don't remotely resemble script files. DOS/cmd doesn't even have real looping structures, just the "for" hack, ERRORLEVEL and/or gotos. Shell scripts have no gotos at all! ...but a forest of much more useful structures cmd never did. I think you'd be better off learning shell scripting instead of trying to "translate" your batch files.

1 Like

I agree!

If you know what the scripts you will have to "translate" do, it is better rewrite them!

And post your questions here!

@Corona688 @felipe.vinturin's

Thank you very much experts. @Corona688 would you please let me know where from i can learn shell scripting? Thank you

Best regards.

You can check this link in the forum which points to some books and excelent links: Featured Books and Articles by Active Forum Members

=o)

1 Like

@felipe.vinturin

Would you please explain what -z stands for? I tried to use different letter and it displays warning message. Thank you very much.

Best

you can look below link for test constructs :wink:

Test Constructs

1 Like

@ygemici

Indeed thank you for the reply. I would like to know, why this if case(especially in my script courtesy felipe.vinturin) listens to few characters? like -p,-z etc and not to -n,-a,-q,.....etc. Is there any meaning for those characters like -n for negation or something like that? Thank you very much.

Best

Try on your box: man test

Lets say, inside a shell script, an if statement is a "test" command call.

# man test
# ...
       -z STRING
              the length of STRING is zero
# ...

If you prefer, you can also use:

# var="Hello World"
# echo "The length of var is: [${#var}]"
The length of var is: [11]

of course we want to explain each one by one but list is very long :slight_smile:
you can read the good document instead of and save the url very useful :wink:

Introduction to if

1 Like

Experts,
I am looking equivalent command for call in the unix. I am writing here whatever i have done so far after the batch file.

@ECHO OFF
CLS
IF DEFINED MY_HOME GOTO start ELSE GOTO home_not_set
call %MY_HOME%\bin\setvars.bat

:start
SET home=%cd%
chdir /d %MY_HOME%\app

SET args=%*
call "%java_home%\bin\java" -cp "conf;lib\*" com.myjava.myproject.commans.projectlog.MyLog %args%
GOTO end

:home_not_set
ECHO My_HOME environment variable is not set.

:end
chdir /d %home%
ENDLOCAL

here is what i have i tried so dar

# Riamp test
set riampPath=RIAMP_HOME/bin/setvars.sh
if [ -z "${RIAMP_HOME}" ]
then
	echo "Set environment variable RIAMP_HOME before running this script".
	exit 1
fi

# Riamp test
if [ -z "${RIAMP_HOME}" ]
then
	call "riampPath"
fi

i know unix does not have goto statements. we can use subroutine instead. Thank you.

UNIX provides the special variable HOME which is almost certainly set to your users' home directory already. RIAMP_HOME seems redundant.

The path RIAMP_HOME/bin/... doesn't expand to what you want it to because you didn't put braces around RIAMP_HOME. Try ${RIAMP_HOME}.

Your first test is to test if the home variable is blank, and execute code inside the if statement when true. Your next test is the same -- only execute code in this block if the string is blank. So, it's backwards -- it will never be executed when the string is set. It's also completely redundant, since you tested for that 3 lines ago. You can leave out that "if" completely. (For the record, to check if a string is not blank, you do if [ ! -z "${STRING}" ]

Shell script doesn't have "call". It has . or source instead (they are synonyms).

"call" is not a subroutine and never has been, though if you treat it carefully and squint a little it's kind of similar. UNIX shells, more modern ones anyway, have actual subroutines:

function do_something
{
        echo $1
}

do_something "qwertyuiop"

And lastly, you'd still be much better off rewriting from scratch than "translating" a batch file. Tons of things that could only be accomplished in CMD via diabolical tricks are easy one-liners in a real shell -- your big library of batch routines may be mostly redundant now. Just naively translating them like this won't teach you as much about real shell scripting, and will probably encourage some bad shell-programming habits.

1 Like

@Corona688

Thank you very much Corona688. I really appreciate. I am working on some java development stuff as well. This would be time consuming if I write from scratch now. That is why i am writing corresponding code for the same. I would definitely try to write shell scripts though.

Best regards.