#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)filecheck 3.2.0 11.09.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Do some actions with files. #@ #@Usage: filecheck [options] filename #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version: Displays the version of the script. #@ -x : Test if filename is executable. If filename can't be found, try to find it in path. #@ -b : Test if filename is a binary file. #@ -f : Test if filename exists and is a regulary file. #@ -l : Test if filename exists and is a symbolic link. #@ -w : Test if filename is writable. #@ -d : Test if filename is a directory. #@ -z : Test if filename is a zip file and not corrupt. #@ -s : Test if filename exists and is not empty. #@ -sl : Test if filename exists and has more than 1 line. #@ -rm : Test if filename exists and then remove it. #@ -fc : Test if filename was accessed no longer than 179 minutes ago (ca. 3 hours). #@ -fy : Test if filename was created no longer than 1339 minutes ago (ca. 1 day). #@ -fw : Test if filename was created no longer than 10079 minutes ago (ca. 1 week). #@ -fmax : Test if filename was modified no longer than 29 minutes ago. #@ -fmin : Test if filename was modified no longer than 15 minutes ago. #@ -html : Test if filename is valid html. #@ -json : Test if filename is valid json. #@ Filename: Name of file to test. # # Update history: # # V 1.0.0 26.10.2017 New version # V 1.0.1 15.07.2018 More options # V 2.0.0 09.07.2019 Also look for executable files in $HOME/bin # V 3.0.0 27.04.2020 Renamed script from filetest to filecheck (filetest is a build in cmd) # V 3.0.1 19.05.2020 Return "./$file" if $file is in local dir # V 3.0.2 11.06.2020 Using library # V 3.1.0 05.06.2023 New copyright # V 3.1.1 07.09.2023 Check if alias exists with option -x # V 3.1.2 27.07.2024 Additional options # V 3.2.0 11.09.2024 New minor version # V 3.2.1 10.10.2024 More options # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Returns true if file is regular file and not empty # We have to test this with 'wc' because -s option won't work on Solaris function FileNotEmpty() { local filename=${1} local bytes=0 if [ -f "$filename" ]; then bytes=`cat "$filename" | wc -c` fi if [ $bytes -gt 0 ]; then echo true else echo false fi } # Return number of lines in file function CountLines() { local filename=${1} local lines=0 if [ -f "$filename" ]; then lines=`cat "$filename" | wc -l` fi echo $lines } # Preset filnam="" option="" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; *) shift paramck=`echo "$pname" | grep '^-'` # Types don't begin with '-' if [ "$paramck" != "" ]; then if [ "$option" = "" ]; then option=`echo "$pname" | tolower` else errstr="Option '$option' already specified. Unknown additional option '$pname'." fi else if [ "$filnam" = "" ]; then filnam="$pname" else errstr="Filename '$filnam' already specified. Unknown additional parameter: '$pname'." fi fi esac done # Plausibility check if [ "$option" = "" -o "$filnam" = "" ]; then errstr="Please specify an option and a filename e.g. '$progstr -x yum'" fi # Display help or error message DisplayHelp case "$option" in -x) result=`alias | grep "alias ${filnam}=" | cut -d"'" -f2 | cut -d" " -f1` if [ "$result" != "" ]; then filnam="$result" fi if [ -f "$filnam" -a -x "$filnam" ]; then # We have to check if it is a file first - otherwise it may is a directory dname=`dirname "$filnam"` if [ "$dname" = "." ]; then dname=`pwd` bname=`basename "$filnam"` echo "${dname}/$bname" else echo "$filnam" fi else # '2>/dev/null' needed for Linux, sed needed for Solaris 10 resstr=`which "$filnam" 2>/dev/null | sed 's|^no '$filnam' in .*||'` if [ "$resstr" != "" ]; then echo "$resstr" fi fi ;; -f) if [ -f "$filnam" ]; then echo "$filnam" fi ;; -l) if [ -L "$filnam" ]; then echo "$filnam" fi ;; -w) if [ -w "$filnam" ]; then echo "$filnam" fi ;; -b) if [ `FileNotEmpty "$filnam"` = true ]; then file=`which file 2>/dev/null | sed 's|^no '$filnam' in .*||'` if [ "$file" != "" ]; then resstr=`"$file" -b --mime-encoding "$filnam"` else resstr=`grep '' "$filnam" | cut -d' ' -f 1 | tolower` fi if [ "$resstr" = "binary" ]; then echo "$filnam" fi fi ;; -d) if [ -d "$filnam" ]; then echo "$filnam" fi ;; -z) if [ `FileNotEmpty "$filnam"` = true ]; then unzip=`which unzip 2>/dev/null | sed 's|^no '$filnam' in .*||'` if [ "$unzip" != "" ]; then $unzip -t "$filnam" >/dev/null 2>&1 stat=$? if [ $stat -eq 0 ]; then echo "$filnam" fi fi fi ;; -s) if [ `FileNotEmpty "$filnam"` = true ]; then echo "$filnam" fi ;; -sl) if [ `CountLines "$filnam"` -gt 1 ]; then echo "$filnam" fi ;; -rm) if [ -f "$filnam" ]; then rm -f "$filnam" 2>/dev/null exitcode=$? fi ;; # amin = access tine, mmin = modification time, cmin = change time, Bmin = Creation time (inode creation) # Get infos about the file with: stat $filnam -fc) minutes=179 if [ "$OS" = "Darwin" ]; then resstr=`find "$filnam" -type f -Bmin -$minutes 2> /dev/null` else # assuming that noatime is set for disk -> then we get creation date in atime resstr=`find "$filnam" -type f -amin -$minutes 2> /dev/null` fi if [ "$resstr" != "" ]; then echo "$filnam" fi ;; -fy | -fw) if [ "$option" = "-fy" ]; then minutes=1339 else minutes=10079 fi resstr=`find "$filnam" -type f -cmin -$minutes 2> /dev/null` if [ "$resstr" != "" ]; then echo "$filnam" fi ;; -fmin | -fmax) if [ "$option" = "-fmin" ]; then minutes=15 else minutes=29 fi resstr=`find "$filnam" -type f -mmin -$minutes 2> /dev/null` if [ "$resstr" != "" ]; then echo "$filnam" fi ;; -html) if [ `FileNotEmpty "$filnam"` = true ]; then file=`which file 2>/dev/null | sed 's|^no '$filnam' in .*||'` if [ "$file" != "" ]; then resstr=`"$file" -b --mime-type "$filnam"` if [ "$resstr" = "text/html" ]; then echo "$filnam" fi fi fi ;; -json) if [ `FileNotEmpty "$filnam"` = true ]; then jq=`which jq 2>/dev/null | sed 's|^no '$filnam' in .*||'` if [ "$jq" != "" ]; then cat "$filnam" | "$jq" >/dev/null 2>&1 stat=$? if [ $stat -eq 0 ]; then echo "$filnam" fi fi fi ;; *) errormsg 1 "($progstr) Invalid option '$option'." exitcode=1 esac exit $exitcode