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.
find
(as most commands) prints errors on stderr , so you can separate them from the output using, eg 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.