LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2015, 10:16 PM   #1
dmunkz
LQ Newbie
 
Registered: Jun 2015
Distribution: rpm based
Posts: 13

Rep: Reputation: Disabled
Question Unexpected end of file


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!

Last edited by dmunkz; 06-07-2015 at 10:40 PM. Reason: typo .. corrected it.
 
Old 06-07-2015, 11:56 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Fix the indentation. Bad:
Code:
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
 
1 members found this post helpful.
Old 06-08-2015, 12:05 AM   #3
dmunkz
LQ Newbie
 
Registered: Jun 2015
Distribution: rpm based
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hello,

Are you saying that is whats wrong? Tried cleaning it up, no change.

Thanks though.
 
Old 06-08-2015, 12:22 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by NevemTeve View Post
Fix the indentation. Bad:
Code:
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
There's also an extra 'if' in there. I'd be inclined to completely rework you conditionals though, as the logic can be simplified quite a bit. i.e. no need for the separate if crit eq 0 / if warn eq 0.
pseudo code:
Code:
if crit gt 0
  list_crit
elif warn gt 0
  list_warn
else
  no_stale_files
fi

Last edited by GazL; 06-08-2015 at 12:27 AM.
 
1 members found this post helpful.
Old 06-08-2015, 12:35 AM   #5
dmunkz
LQ Newbie
 
Registered: Jun 2015
Distribution: rpm based
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
There's also an extra 'if' in there. I'd be inclined to completely rework you conditionals though, as the logic can be simplified quite a bit. i.e. no need for the separate if crit eq 0 / if warn eq 0.
pseudo code:
Code:
if crit gt 0
  list_crit
elif warn gt 0
  list_warn
else
  no_stale_files
fi

Hello,

That did it!

Code:
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 at over $TWARN mins old:\n"
        printf '%s\n' "${WARN[@]}"
        exit 2
elif [ ${#WARN[@]} -gt 0 ];then
        printf "WARN -- The following files are over $TWARN mins old:\n"
        printf '%s\n' "${WARN[@]}"
        exit 1
else
        printf "OK -- No stale files.\n"
fi

Thanks for the pointer!
 
Old 06-08-2015, 12:40 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
You're welcome.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected end of file JankAle Linux - Newbie 5 12-12-2013 05:32 AM
[SOLVED] Unexpected end of file Kubik Linux - Newbie 5 10-07-2012 07:06 PM
unexpected end of file btap Programming 14 09-30-2012 12:19 PM
Running a Crafted bash script yelds 'Unexpected end of file' at the end of the file MCLover1337 Linux - General 5 10-15-2011 08:29 AM
Unexpected end of file??? raid517 Linux - Software 2 12-01-2004 04:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration