Bash File Is Running Fine in Windows for Testng But It Is Not Working in Linux/Mac

Bash file is running fine in windows for testng but it is not working in linux/mac

Paths are separated using : under Unix-like systems and not ; as in Windows:

java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml

If you are using bash under Windows then changing to : should work everywhere.

The ; character means end of statement to a Unix shell, so what you are attempting to exceute is:

java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml

Launch TestNG suite from bash script

After a couple of days I finally solved the problem by making a main class in my Java project calling the XML file, then create an executable jar of my project and in the end create a bash script calling this jar.

The code of my .sh is here (quite simple) :

java -jar Exec.jar

for loop is not giving expected output in sh whereas it is working fine if sh #!/bin/bash +x is added to the script block

@alaniwi already explained in his comment the error in your rm command. You are trying to remove a file with the name $newfile, and you probably don't have any file starting with a Dollar sign.

The other problem is similar, but not identical: Your grep command searches for a literal string $newfile, while you probably want to search for a string stored in the variable newfile. Hence you have to drop the \.

But this still means that the content of the variable newfile is subject to interpretation as a regular expression. For example, if newfile has the value abc.txt, grep would also succeed if new_changes.txt contained just abcdtxt. To avoid this error, you should use the -F option to grep, to avoid interpretation as a regexp.

And still, there is one more error: Say newfile has the value abc, and new_changes.txt contained just xxxabc, but they would still match, since abc is a substring of xxxabc. To avoid this error, you should use the -x option to grep, which forces to match the whole line.

Hence, your command should be grep -qFx "$newfile" new_changes.txt

How to check if running in Cygwin, Mac or Linux?

Usually, uname with its various options will tell you what environment you're running in:

pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

pax> uname -s
CYGWIN_NT-5.1

And, according to the very helpful schot (in the comments), uname -s gives Darwin for OSX and Linux for Linux, while my Cygwin gives CYGWIN_NT-5.1. But you may have to experiment with all sorts of different versions.

So the bash code to do such a check would be along the lines of:

unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo ${machine}

Note that I'm assuming here that you're actually running within CygWin (the bash shell of it) so paths should already be correctly set up. As one commenter notes, you can run the bash program, passing the script, from cmd itself and this may result in the paths not being set up as needed.

If you are doing that, it's your responsibility to ensure the correct executables (i.e., the CygWin ones) are being called, possibly by modifying the path beforehand or fully specifying the executable locations (e.g., /c/cygwin/bin/uname).

Unexpected Shell Script errors

I think this is due to having the wrong line ending character, which would be the case if you are running sublime in windows.

Try running od -xcb to find the carriage return characters. Unix expects just \n whereas you will see \r\n with windows carriage returns.

You can fix your file by running dos2unix generate.sh.

As per Dannys comment below you can change the settings in sublime by going to view > line endings > unix.


Examples:

I copied your script and created a file using vim with unix carriage returns called script.sh. I then used unix2dos to create a file with windows carriage returns for testing and was able to recreate your error.

Unix carriage returns:

od -xcb script.sh
0000000 2123 622f 6e69 622f 7361 0a68 6365 6f68
# ! / b i n / b a s h \n e c h o

Windows carriage returns:

od -xcb windows.sh
0000000 2123 622f 6e69 622f 7361 0d68 650a 6863
# ! / b i n / b a s h \r \n e c h

And the error from the windows script:

> . windows.sh

Please enter your student ID:
': not a valid identifier
Please enter your MySQL password:
': not a valid identifier
DB username is db12
That has been completed

How can I check if a program exists from a Bash script?

Answer

POSIX compatible:

command -v <the_command>

Example use:

if ! command -v <the_command> &> /dev/null
then
echo "<the_command> could not be found"
exit
fi

For Bash specific environments:

hash <the_command> # For regular commands. Or...
type <the_command> # To check built-ins and keywords

Explanation

Avoid which. Not only is it an external process you're launching for doing very little (meaning builtins like hash, type or command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system.

Why care?

  • Many operating systems have a which that doesn't even set an exit status, meaning the if which foo won't even work there and will always report that foo exists, even if it doesn't (note that some POSIX shells appear to do this for hash too).
  • Many operating systems make which do custom and evil stuff like change the output or even hook into the package manager.

So, don't use which. Instead use one of these:

command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

(Minor side-note: some will suggest 2>&- is the same 2>/dev/null but shorter – this is untrue. 2>&- closes FD 2 which causes an error in the program when it tries to write to stderr, which is very different from successfully writing to it and discarding the output (and dangerous!))

If your hash bang is /bin/sh then you should care about what POSIX says. type and hash's exit codes aren't terribly well defined by POSIX, and hash is seen to exit successfully when the command doesn't exist (haven't seen this with type yet). command's exit status is well defined by POSIX, so that one is probably the safest to use.

If your script uses bash though, POSIX rules don't really matter anymore and both type and hash become perfectly safe to use. type now has a -P to search just the PATH and hash has the side-effect that the command's location will be hashed (for faster lookup next time you use it), which is usually a good thing since you probably check for its existence in order to actually use it.

As a simple example, here's a function that runs gdate if it exists, otherwise date:

gnudate() {
if hash gdate 2>/dev/null; then
gdate "$@"
else
date "$@"
fi
}

Alternative with a complete feature set

You can use scripts-common to reach your need.

To check if something is installed, you can do:

checkBin <the_command> || errorMessage "This tool requires <the_command>. Install it please, and then run this tool again."

How to run TestNG from command line

You need to have the testng.jar under classpath.

try C:\projectfred> java -cp "path-tojar/testng.jar:path_to_yourtest_classes" org.testng.TestNG testng.xml

Update:

Under linux I ran this command and it would be some thing similar on Windows either

test/bin# java -cp ".:../lib/*" org.testng.TestNG testng.xml

Directory structure:

/bin - All my test packages are under bin including testng.xml
/src - All source files are under src
/lib - All libraries required for the execution of tests are under this.

Once I compile all sources they go under bin directory. So, in the classpath I need to specify contents of bin directory and all the libraries like testng.xml, loggers etc over here. Also copy testng.xml to bin folder if you dont want to specify the full path where the testng.xml is available.

 /bin
-- testng.xml
-- testclasses
-- Properties files if any.
/lib
-- testng.jar
-- log4j.jar

Update:

Go to the folder MyProject and type run the java command like the way shown below:-

java -cp ".: C:\Program Files\jbdevstudio4\studio\plugins\*" org.testng.TestNG testng.xml

I believe the testng.xml file is under C:\Users\me\workspace\MyProject if not please give the full path for testng.xml file



Related Topics



Leave a reply



Submit