Hello,
I am trying to sort out why the below bash script will not work. Reading over at
Bash-Beginners-Guide, it seems like I am closing the constructs correctly. However, I continue to get errors.
So, first the script itself :
Code:
#!/bin/bash
# Checks for file age OLDER than.
# Functions
usage()
{
cat << EOF
USAGE ::
-h| --help
Print this out.
-p| --path
Takes globing. /path/to/*/*/dir will go into
/path/to/{path1,path2,path3}/{tom,jarry}/INBOX
-d| --maxdepth
We check N levels deep from target path as
/path/to/{path1,path2,path3}/{tom,jarry}/INBOX/{LEVEL1,LEVEL2}/
-w| --warnage
Will return WARN state to nagios is older than this number.
-c| --criticalage
Will return CRITICAL is older than this age.
EXAMPLE :: test -p /some/path/*/*/target/path -d 2
EOF
}
# Print usage if nothing is passed. I should figure out
# how to do this with anything that is not passed.
#if [ $? !=0 ] ; then usage >&2 ; exit 1 ; fi
# Zero out args here.
SPATH=
MDEPTH=
TWARN=
TCRICAL=
# Reading in passed options
# I have not set sanity checks
# Will once I figure it out.
while getopts "hp:d:w:c:" opt
do
case $opt in
h )
usage
exit
;;
p )
SPATH=$OPTARG
;;
d )
MDEPTH=$OPTARG
;;
w )
TWARN=$OPTARG
;;
c )
TCRICAL=$OPTARG
;;
esac
done
if [[ -z $SPATH ]] || [[ -z $MDEPTH ]] || [[ -z $TWARN ]] || [[ -z $TCRICAL ]]
then
usage
exit 1
fi
# Setting up the arrays to figure out what is warn / crit
## Warn state
WARN=()
while IFS= read -d $'\0' -r file ; do
WARN=("${WARN[@]}" "$file")
done < <(find $SPATH -maxdepth $MDEPTH -type f -mmin +$TWARN -mmin -$TCRICAL -print0)
## Critical state
CRIT=()
while IFS= read -d $'\0' -r file ; do
CRIT=("${CRIT[@]}" "$file")
done < <(find $SPATH -maxdepth $MDEPTH -type f -mmin +$TCRICAL -print0)
# Putting it in motion
if [ ${#CRIT[@]} -eq 0 ]; then
if [ ${#WARN[@]} -eq 0 ]; then
printf "OK -- No stale files.\n"
exit 0
fi
else
if [ ${#CRIT[@]} -gt 0 ]; then
printf "CRITICAL -- The following files are over $TCRICAL mins old:\n"
printf 's\n' "${CRIT[@]}"
printf "NOTE -- The following meet limits set for WARN:\n"
printf '%s\n' "${WARN[@]}"
exit 2
elif
if [ ${#WARN[@]} -gt 0 ]; then
printf "WARN -- The following meet limits set for WARN:\n"
printf '%s\n' "${WARN[@]}"
exit 1
else
printf "UNKNOWN -- We got to the end but got lost.\n"
exit 3
fi
fi
Now, I ran the above using bash -x to see if the command line args got expanded the way I thought they should. Of course, it breaks in the same spot, down at the end.
Code:
bash -x ./check_file_age -p /home/fauz -d 2 -w 30 -c 60
+ SPATH=
+ MDEPTH=
+ TWARN=
+ TCRICAL=
+ getopts hp:d:w:c: opt
+ case $opt in
+ SPATH=/home/fauz
+ getopts hp:d:w:c: opt
+ case $opt in
+ MDEPTH=2
+ getopts hp:d:w:c: opt
+ case $opt in
+ TWARN=30
+ getopts hp:d:w:c: opt
+ case $opt in
+ TCRICAL=60
+ getopts hp:d:w:c: opt
+ [[ -z /home/fauz ]]
+ [[ -z 2 ]]
+ [[ -z 30 ]]
+ [[ -z 60 ]]
+ WARN=()
+ IFS=
+ read -d '' -r file
++ find /home/fauz -maxdepth 2 -type f -mmin +30 -mmin -60 -print0
+ CRIT=()
+ IFS=
+ read -d '' -r file
++ find /home/fauz -maxdepth 2 -type f -mmin +60 -print0
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
+ CRIT=("${CRIT[@]}" "$file")
+ IFS=
+ read -d '' -r file
./check_file_age: line 108: syntax error near unexpected token `fi'
./check_file_age: line 108: `fi'
I have been reading all over the place but have yet to figure this out. This is what I always come against when trying to do nested test. I am sure I am missing simple. At least, I hope so.
Any help would be greatly appreciated!