Validata correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Illegal number of parameters ($#, should be 2)"
exit 1
fi
$# returns number of parameters passed to script. If it’s less then 2, script ends with error.
Check if environment variable is set
if [ -z "$CLEANING_JOB" ]
then
echo "Environmental variable [CLEANING_JOB] does not exist."
exit 1
fi
Bash function example
set_dir_structure() {
YEAR=$(date +"%Y" "$@")
MONTH=$(date +"%m" "$@")
DAY=$(date +"%d" "$@")
HOUR=$(date +"%H" "$@")
retval="$YEAR/$MONTH/$DAY/$HOUR"
}
This function can be executed with or without parameter to get proper dir structure related with date and time. Examples:
# return dir structure with current hour
set_dir_structure
mkdir -p $ROOT_DIR_PATH/$retval
# return dir structure with previous hour
set_dir_structure --date="last hour"
mkdir -p $ROOT_DIR_PATH/$retval
# return dir structure with next hour
set_dir_structure --date="next hour"
mkdir -p $ROOT_DIR_PATH/$retval