0

Any help is appreciated:

I am running a simple java currency converter application and trying to test it using conditional bash statements. For this test I am seeking to test when the user doesn't enter any value into the application what the resulting output is. Here is the bash script:


#!/bin/bash

input=""

expectedOuput="Please enter correct input"

actualOutput=$(java CurrencyConverter $input)

if [ $expectedOutput == $actualOutput ]; then
echo "Test 1 Passed"
else
echo "Test 1 Failed"
fi

I am aiming for the test to fail and echo this, however the script just returns the java error instead of echoing, like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at CurrencyConverter.main(CurrencyConverter.java:12)

I am newer to bash scripting so have been changing the syntax, I just cant seem to stop the java error being printed

1 Answer 1

1

The exception message is printed to stderr, so if you want to get it in actualOutput (perhaps misleading name now):

actualOutput=$(java CurrencyConverter "$input" 2>&1)

you can also check the exit status of the java command.

Also check misspelled expectedOuput and use of quotes. Use https://www.shellcheck.net/.

And the ArrayIndexOutOfBoundsException it's definitely something you should fix in your java code.

Sign up to request clarification or add additional context in comments.

3 Comments

Diego, I appreciate you're help greatly, thank you! After reading your comment, I have changed my script to this: `` #!/bin/bash input="" expectedOutput="Please enter correct input" actualOutput=$(java CurrencyConverter "$input" 2>&1) if [ $expectedOutput == $actualOutput ]; then echo "Test 1 Passed" else echo "Test 1 Failed" fi `` I am glad that the java error is no longer printing, however there is still an error: test1.sh: 9: [: Please: unexpected operator I am confused as "==" should compare strings, unless I am looking in the wrong area? Thanks, Ewan
Sorry, for the poor formatting in my last comment. I tried with one equal operand and this seems to work, perhaps I am confusing syntax! Thanks for your help Diego and have a nice weekend :)
I guess ` if [[ "$expectedOutput" == "$actualOutput" ]]; then...`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.