Score:0

How to create a config for your find and delete script

cn flag

I have a script that does the following task: Search in a directory for a file with the determined criteria.

find Documents/Files/Logfiles/ -mtime +1d -type f '(' -name '*.log' -or -name '*[0-9].*' ')' -delete

now I'd like to create a config file for this script where you can detriment, the extension, the modification time or the file name yourself.

I've never created a config file before and am very new to zsh, so I have no idea how to do that. the challange here is that we are supposed to work as this file is for a client.

hr flag
Are you (or your client) using `macosx` as implied by your tag?
Mahmoud avatar
cn flag
yes both me and the client are using macosx
sudodus avatar
jp flag
Which version of Ubuntu and which version of `find` are you running?
Mahmoud avatar
cn flag
I'm currently working on a macos, using iterm2. as for the version of find I don't know. though I know im running zsh 5.8 if that helps.
sudodus avatar
jp flag
You should be able to print the version with `find -version`. I ask because your syntax for `mtime` does not work for me.
Mahmoud avatar
cn flag
typing ```fins -version```results for me in ```find: illegal option -- v usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression] find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]```
sudodus avatar
jp flag
I think you should ask at some website for macos because `find` works different from here at Ubuntu.
terdon avatar
cn flag
You could ask on [unix.se] or [apple.se] but if you do, please clarify why you want a config file. That is a very complicated and hard to use approach. Why not simply pass the search string as an argument or an option when the script is launched? Using a config file just makes everybody's (yours and your users') lives harder for no benefit.
Mahmoud avatar
cn flag
I just asked on Ask Different. the reason for the config its just that it is a task. on creating the script I mentioned it was optional to have the script lunch from a config file. upon asking how we could do that, we got tasked to figure it out ourself (as a challange) though we couldn't a result that could give us a lead on how to do it. though I appreciate the time and effort in helping us!
Score:0
bz flag

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.*" ")"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.