#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)filecheck 3.1.0 05.06.2023 (c)2023 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. #@ -f : Test if filename exists. #@ -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 created no longer than 179 minutes ago. #@ -fmax : Test if filename was modified no longer than 29 minutes ago. #@ -fmin : Test if filename was modified no longer than 15 minutes ago. #@ 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 # script=${0} # Name of this script progstr=`basename "$script"` # Basename of the script progdir=`dirname "$script"` # Dirname of the script # 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 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 # Set PATH to something useful if [ "$WORKPATH" != "" ]; then PATH="$WORKPATH" fi # 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=1 ;; -h | --help) shift showhelp=1 ;; *) paramck=`echo "$1" | grep '^-'` # Types don't begin with '-' if [ "$filnam" = "" -a "$paramck" = "" ]; then filnam=${1} else option=${1} fi shift 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) 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 ;; -d) if [ -d "$filnam" ]; then echo "$filnam" fi ;; -z) if [ -f "$filnam" ]; 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 [ -f "$filnam" ]; then # We have to test this with 'wc' because -s option won't work on Solaris or Ubuntu resstr=`wc -c "$filnam"` fresult=`echo $resstr | cut -d' ' -f1` if [ $fresult -gt 0 ]; then echo "$filnam" fi fi ;; -sl) if [ -f "$filnam" ]; then resstr=`wc -l "$filnam"` fresult=`echo $resstr | cut -d' ' -f1` if [ $fresult -gt 1 ]; then echo "$filnam" fi fi ;; -rm) if [ -f "$filnam" ]; then rm -f "$filnam" fi ;; # amin = access tine, mmin = modification time, cmin = change time, Bmin = Creation time (inode creation) -fc) if [ "$OS" = "Darwin" ]; then resstr=`find "$filnam" -type f -Bmin -179 2> /dev/null` else # assuming that noatime is set for disk -> then we get creation date in atime resstr=`find "$filnam" -type f -amin -179 2> /dev/null` fi if [ "$resstr" != "" ]; then echo "$filnam" fi ;; -fmax) resstr=`find "$filnam" -type f -mmin -29 2> /dev/null` if [ "$resstr" != "" ]; then echo "$filnam" fi ;; -fmin) resstr=`find "$filnam" -type f -mmin -15 2> /dev/null` if [ "$resstr" != "" ]; then echo "$filnam" fi ;; *) errormsg 1 "($progstr) Invalid option '$option'." exit 1 ;; esac exit $exitcode