Skip to content

Commit

Permalink
Merge pull request #1 from richbl/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
richbl committed Jan 19, 2022
2 parents 0ac7948 + c7e1f13 commit 90ad929
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@

`run_old_files_delete.sh` is a related script intended to be used for making unattended script calls into `old_files_delete.sh` (*e.g.*, running cron jobs).

## [<img src="https://cloud.githubusercontent.com/assets/10182110/18208786/ae5d76b2-70e5-11e6-9663-cfe47d13f4d9.png" width="150" />](https://github.com/richbl/a-bash-template)Developed with a Bash Template (BaT)
## [Developed with a Bash Template (BaT)](https://github.com/richbl/a-bash-template)[<img src="https://user-images.githubusercontent.com/10182110/145758715-b127adfc-710b-49d3-9ede-151adc83ae76.png" width="150" />](https://github.com/richbl/a-bash-template)

**Old-Files-Delete** uses a bash template (BaT) called **[A-Bash-Template](https://github.com/richbl/a-bash-template)** designed to make script development and command line argument management more robust, easier to implement, and easier to maintain. Here are a few of those features:
**Old-Files-Delete** uses a Bash shell template (BaT) called **[A-Bash-Template](https://github.com/richbl/a-bash-template)** designed to make script development and command line argument management more robust, easier to implement, and easier to maintain. Here are a few of those features:

- Dependencies checker: a routine that checks all external program dependencies (*e.g.*, [sshpass](http://linux.die.net/man/1/sshpass) and [jq](https://stedolan.github.io/jq/))
- Dependencies checker: a routine that checks all external program dependencies (*e.g.*, [jq](https://stedolan.github.io/jq/))
- Arguments and script details--such as script description and syntax--are stored in the [JSON](http://www.json.org/) file format (*i.e.*, `config.json`)
- JSON queries (using [jq](https://stedolan.github.io/jq/)) handled through wrapper functions
- A script banner function automates banner generation, reading directly from `config.json`
- Command line arguments are parsed and tested for completeness using both short and long-format argument syntax (*e.g.*, `-u|--username`)
- Command line arguments are parsed and tested for completeness using both short and long-format argument syntax (*e.g.*, `-f|--font`)
- Optional command line arguments are permissible and managed through the JSON configuration file
- Template functions organized into libraries to minimize code footprint in the main script
- Template functions organized into libraries (see the [Bash-Lib](https://github.com/richbl/bash-lib) project for details) to minimize code footprint in the main script

For more details about using a bash template, [check out the BaT sources here](https://github.com/richbl/a-bash-template).

## Requirements

- An operational [bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) environment (bash 4.3.2 used during development)
- One additional external program:
+ [jq](https://stedolan.github.io/jq/), for parsing the `config.json` file

While this package was written and tested under Linux (Ubuntu 15.10), there should be no reason why this won't work under other Unix-like operating systems.
- An operational [Bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) environment (Bash 5.1.8 used during development)
- One additional external program:
- [jq](https://stedolan.github.io/jq/), for parsing the `config.json` file

While this package was initially written and tested under Linux (Ubuntu 21.10), there should be no reason why this won't work under other shells or Unix-like operating systems that support the `gsettings` application.

## Basic Usage
**Old-Files-Delete** is run through a command line interface, so all of the command options are made available there.
Expand All @@ -35,7 +34,7 @@ Here's the default response when running `old_files_delete.sh` with no arguments

|
| A bash script to recursively delete files older than (n) days
| 0.1.0
| 1.1.0
|
| Usage:
| old_files_delete.sh -d directory -n days_ago
Expand All @@ -55,7 +54,7 @@ When arguments are correctly passed, the script provides feedback on the success

|
| A bash script to delete files older than (n) days
| 0.1.0
| 1.1.0
|
| Usage:
| old_files_delete.sh -d directory -n days_ago
Expand All @@ -79,7 +78,7 @@ The example below represents my own cron job set to run nightly at 00:15 on my R

As configured, I no longer have to worry about motion-captured image files (jpg and avi files) getting generated over time, and eventually consuming available disk space on my IoT device.

## A Note on Cloning: This Project Uses Git Submodules
## >> A Note on Cloning: This Project Uses Git Submodules

This project uses a Git [submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) project, specifically the `bash-lib` folder to keep project libraries up-to-date without manual intervention.

Expand Down
2 changes: 1 addition & 1 deletion bash-lib
Submodule bash-lib updated 3 files
+28 −4 README.md
+58 −70 args
+36 −34 general
45 changes: 29 additions & 16 deletions old_files_delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# -----------------------------------------------------------------------------
#
# A bash script to recursively delete files older than (n) days
Expand All @@ -30,33 +30,46 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# --side-effect(s): older files deleted
#

#
# NOTE:
# The string '[user-config]' is an indication that some user configuration
# may be needed to customize this script
#
# FOR MORE INFORMATION:
# This script was written using the BaT template. To learn more, refer to
# the A-Bash-Template project (https://github.com/richbl/a-bash-template)
#

# -----------------------------------------------------------------------------
# script declarations
# script library sources and declarations
#
shopt -s extglob
EXEC_DIR="$(dirname "$0")"
# shellcheck source=bash-lib/args
EXEC_DIR="$(dirname "$(readlink -f "$0")")"
source "${EXEC_DIR}/bash-lib/general"
source "${EXEC_DIR}/bash-lib/args"

# [user-config] set any external program dependencies here
declare -a REQ_PROGRAMS=('jq')

# -----------------------------------------------------------------------------
# perform script configuration, arguments parsing, and validation
#

check_program_dependencies "${REQ_PROGRAMS[@]}"
display_banner
scan_for_args "$@"
check_for_args_completeness

# -----------------------------------------------------------------------------
# [user-config]
# Any code from this point on is custom code, using the sevices provided
# through this BaT (https://github.com/richbl/a-bash-template) template

# -----------------------------------------------------------------------------
# perform old file delete
#
echo "Deleting old files..."
echo
find $(get_config_arg_value directory) -mtime +$(get_config_arg_value 'days ago') -type f -delete

if [ $? -ne 0 ]; then
if ! find "$(get_config_arg_value directory)" -mtime +"$(get_config_arg_value 'days ago')" -type f -delete; then
echo "Error: file delete did not complete."
quit
else
Expand Down
25 changes: 10 additions & 15 deletions run_old_files_delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# -----------------------------------------------------------------------------
#
# A bash script front-end to call old_files_delete.sh
#
# version: 0.1.0
#
# requirements:
#
# --access to old_files_delete.sh
#
# inputs:
#
# --None (runs with no inputs)
#
# outputs:
#
# --None (side effect is the completion of the called script)
#

Expand Down

0 comments on commit 90ad929

Please sign in to comment.