To use a config file for this script, it imply that the script will have to read some parameter from a config file.
Config file
config.txt:
path=.
extension=sh
modtime=1
filename=test
Script file:
#!/bin/bash
lines=$(<config.txt) #read config.txt
for line in $lines; do # parse each line and extract param-value keys
if [[ $line = *"="* ]]; then # if a word contains an "="...
vars[${line%%=*}]=${line#*=} # then set it as an associative-array key
fi
done
find ${vars[path]} -mtime ${vars[modtime]} -type f "(" -name "*.${vars[extension]}" -or -name "${vars[filename]}.*" ")"
The other solution is to use the script with arguments and to specify path, extension, modification time or the filename as follows:
#!/bin/bash
help()
{
echo ""
echo "Usage: $0 -p Path -e fileExtension -m modificationTime -f fileName"
echo -e "\t-path Path where to search for"
echo -e "\t-ext file name extension"
echo -e "\t-modtime modification time e.g. +1d "
echo -e "\t-filename file name"
exit 1 # Exit script after printing help
}
while getopts "p:e:m:f:" opt
do
case "$opt" in
p ) pathFile="$OPTARG" ;;
e ) extension="$OPTARG" ;;
m ) modifyTime="$OPTARG" ;;
f ) fileName="$OPTARG" ;;
? ) help ;; # Print help in case parameter is non-existent
esac
done
# Print help in case parameters are empty
if [ -z "$pathFile" ] || [ -z "$extension" ] || [ -z "$modifyTime" ] || [ -z "$fileName" ]
then
echo "Empty parameter(s), please check them...";
help
fi
find $pathFile -mtime $modifyTime -type f "(" -name "*.$extension" -or -name "$fileName.*" ")"