Unable to redirect output to a file

I m not able to redirect the java version to a file however, it shows as output when I run my script.

bash-3.2$ more 1test.tmp
java_version=`which java`
echo "MY JAVA:"$java_version
version=`"$java_version" -version`
echo $version  >>/tmp/moht/java_version.log
$java_version -version 2 >>/tmp/moht/java_version.log

Output:

But I do not see any information logged to the file bash-3.2$ more /tmp/moht/java_version.log

I m using ksh shell.

Can you please suggest?

I believe your last line produces no errors and therefore truncates your output file.
tried to debug with set -x yet?

1 Like

Here is the output after adding set -x

Can you please tell me why is the output not getting redirected in the file ?

Hi,
When you redirect file descriptor (as 2), you don't add space between fd and redirect...
So, try with:

version=`"$java_version" -version 2>&1`

and/or

$java_version -version 2>>/tmp/moht/java_version.log
1 Like

Worked !! many thanks :b: