Home > OS >  Trouble with importing Java output into shell script
Trouble with importing Java output into shell script

Time:11-11

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

CodePudding user response:

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.

  • Related