1

I have a java project with unit tests that I am developing on a Linux system, so I made a bash script to run all my tests. I can't really get it to work though

#!/bin/bash
JUNITPATH="/usr/share/java/junit4-4.11.jar"
TESTCLASSES=$(find kai/modsogner_och_durin/testing/* | grep .class$)
EXT="class"
CLASSNAMES=${TESTCLASSES%.$EXT}
echo "BEFORE: $CLASSNAMES"
DOTS=$CLASSNAMES | tr '/' '.'
echo AFTER: $DOTS
java -cp .:$JUNITPATH org.junit.runner.JUnitCore $DOTS 

The echo statements and the DOTS variable are obviously only for debugging purposes. My problem is that when I try to assign to DOTS, when I check DOTS it is empty.

echo AFTER: $DOTS

this prints an empty line, but the BEFORE: print prints the path perfectly. It seems like I am simply not very good with bash and am missing something very obvious, but I really can't get it to work. I did at least make sure that if I replace

DOTS=$CLASSNAMES | tr '/' '.'

with

echo $CLASSNAMES | tr '/' '.'

it prints the class names with dot separated packages as intended. So it seems like a syntax error.

4
  • 2
    I would strongly encourage you to use a pre-existing tool to run your tests, such as Maven. Commented Jan 12, 2015 at 17:31
  • these are junit test so you can use ant as well, can even have stop on failure then and use jacoco to get coverage reports Commented Jan 12, 2015 at 17:32
  • I heard about maven/ant and have been considering looking into it. Haven't gotten around though, partly because I am new to the whole unittest thing, but also because I wanted to learn how to actually do it myself before I let other software do it for me. This is just a pretty basic university course anyway, so I can afford to play around for now. Thanks for the tip though! Commented Jan 12, 2015 at 17:49
  • Ant doesn't really "do it for you" All it does it just runs the unit test like it would be ran. Only difference is its in an xml parsed file. Commented Jan 12, 2015 at 18:10

1 Answer 1

3

Regular assignment like FOO=bar baz expects a string and assigns it to the variable; it will not run any commands.

What you want is command substitution, via $(...) (or the old-fashioned `...`):

DOTS=$(echo $CLASSNAMES | tr '/' '.')

But in this simple case, you can also use bash's parameter substitution with ${varname//pattern/replacement}:

DOTS=${CLASSNAMES//\//.}
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick! I used the bash substitution thingy. The keywords "parameter substitution" and "command substitutions" were really helpful also!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.