2

I have a shell script that is getting versions of Tomcat installed on my system. I am able to print found versions to stdout using the find command. However, I am also getting output for directories and files that are not found. How can I remove this from the output and only show found files in output? Code and output is below.

Script:

#!/bin/sh
  
APP="Apache Tomcat"

# Common install paths to iterate over
set -- "/opt" "/usr/share" "/var" "/var/lib" "/usr" "/usr/local" 

if [ -x "$(command -v unzip)" ]; then
    for _i in "$@"; do
        find_bootstrap=$(find $_i/tomcat*/bin/ -name bootstrap.jar)
        for found in $find_bootstrap; do
            get_version=$(unzip -p $found META-INF/MANIFEST.MF | grep -Eo 'Implementation-Version: [0-9].[0-9]?.[0-9]?[0-9]?' | grep -Eo '[0-9].[0-9]?.[0-9]?[0-9]?')
            echo "$APP $get_version"
        done
    done
fi

Output:

Apache Tomcat 7.0.82
Apache Tomcat 8.5.30
Apache Tomcat 9.0.31
find: ‘/var/tomcat*/bin/’: No such file or directory
find: ‘/var/lib/tomcat*/bin/’: No such file or directory
find: ‘/usr/tomcat*/bin/’: No such file or directory
find: ‘/usr/local/tomcat*/bin/’: No such file or directory

Is there a way that I can get rid of find: ‘/var/tomcat*/bin/’: No such file or directory if no file is found.

1 Answer 1

1

find (as most commands) prints errors on stderr, so you can separate them from the output using, e.g. a redirection:

find_bootstrap=$(find $_i/tomcat*/bin/ -name bootstrap.jar 2>/dev/null)

Remark: To find out all installations of Tomcat I would rather look for catalina.jar (less chances for a name conflict) which would give:

while IFS= read -r -d $'\0' jar; do
    version=$(unzip -p "$jar" META-INF/MANIFEST.MF | grep -oP '(?<=Implementation-Version: )\d+.\d+.\d+');
    echo Apache Tomcat $version
done < <(find /usr /var /opt -name 'catalina.jar' -print0 2>/dev/null)
Sign up to request clarification or add additional context in comments.

Comments

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.