I am trying to write a shell script to check if a command executed successfully or not in rhel 7 and finding the installed tomcat version.
I am using below script.
var4=$(find / -name "catalina.jar" ! -size 0 |egrep -v "tmp|docker|WinSxS|Permission|HISTORY|alternatives|bearer11ssl|manifest")
i=`which java`
for loc4 in $var4
do
abc=`$i -classpath $loc4 org.apache.catalina.util.ServerInfo`
xyz="$abc"
status=$?
if [ $status -eq 0 ];then
# tomcat_version=`grep -w "Server number:" $xyz`
#$xyz > /tmp/tomcat
echo -e "{\"'tomcat'\":\"'"$xyz"'\"}"
fi
done
But when i am trying to execute script it's displaying the output info even if the command not executed successfully.
Output :
Error: Could not find or load main class org.apache.catalina.util.ServerInfo
{"'tomcat'":"''"}
{"'tomcat'":"'Server version: Apache Tomcat/9.0.30 Server built: Dec 7 2019 16:42:04 UTC Server number: 9.0.30.0 OS Name: Linux OS Version: 3.10.0-1062.9.1.el7.x86_64 Architecture: amd64 JVM Version: 1.8.0_171-b11 JVM Vendor: Oracle Corporation'"}
$abc can't be executed as it is a variable holding the result of the "command substitution". If you want to check if that was OK, move the status assignment one up, then:
It might be that you are not getting output but actually it is errors being displayed. Standard output (good things you want to see) are written to file descriptor 1. Standard errors (bad things that you usually want to see) are written to file descriptor 2. Both of these are usually directed to the screen, but I think you are wanting to ignore some of them.
You might try directing your error output elsewhere. Some examples of this could be:-
some_command 2> /tmp/error_file # Save the errors to this file
some_command 2> /dev/null # Actually I don't care, just bin them
some_command 2> &1 # Redirect errors to standard output
This last one can be useful if you want to consider the messages returned.
My intention is when ever I run the below command, if the output is successful then I want to print successful output need to ignore if the command output is not successfull.
But I tried to redirect the output but still getting error messages.
Error: Could not find or load main class org.apache.catalina.util.ServerInfo
{"'tomcat'":"'Server version: Apache Tomcat/9.0.30 Server built: Dec 7 2019 16:42:04 UTC Server number: 9.0.30.0 OS Name: Linux OS Version: 3.10.0-1062.9.1.el7.x86_64 Architecture: amd64 JVM Version: 1.8.0_171-b11 JVM Vendor: Oracle Corporation'"}
Can you show us how you tried to redirect the output?
Please wrap your commands and the output in CODE tags. To do this, you can simply highlight your text that is code or data and then click on the </> in the editing menu. (You can also type code tags