If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Script count files with error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-10, 07:12
sant sant is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Script count files with error

Hello

I'm trying to run this script count_files:

WARNING_STATE=1
CRITICAL_STATE=2
file_count=0file_count=`ls /number/files/ | wc -l`if [ $file_count -gt 15 ]
then
echo "WARNING:" $file_count "files"
exit $WARNING_STATE
fiif [ $file_count -gt 25 ]
then
echo "CRITICAL:" $file_count "files"
exit $CRITICAL_STATE
fi
echo "OK:" $file_count "files"
exit $OK_STATE

The result should gives a number of files, but after many tests it doesn't work ok
it always returns this error:

./count_files: [: : integer expression expected
./count_files: line 11: syntax error near unexpected token `then'
./count_files: line 11: `then'

Can someone help me?

Thank you for advanced
Reply With Quote
  #2 (permalink)  
Old 01-25-10, 07:42
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by sant View Post
Can someone help me?
Various issues:
  • fiif is not a command
  • Setting file_count to 0 does nothing in this case as the value is overwritten immediately
  • it's always worth echoing out the value of variables just to see what's returned ie echo $file_count
  • You have multiple commands all joined into one command ie line 3
  • OK_STATE is not set anywhere
  • Indenting your code and separating the lines makes it easier to read
  • it's also a good idea to state what type of shell you are using ie /bin/ksh etc
Quote:
Originally Posted by sant
but after many tests it doesn't work ok it always returns this error:
Did you try altering the code or fixing any of the syntax errors between tests?
My best guess:
Code:
OK_STATE=0
WARNING_STATE=1
CRITICAL_STATE=2

file_count=`ls /number/files/ | wc -l`

if [ $file_count -gt 15 ]
then
   echo "WARNING: $file_count files"
   exit $WARNING_STATE
fi

if [ $file_count -gt 25 ]
then
   echo "CRITICAL: $file_count files"
   exit $CRITICAL_STATE
fi

echo "OK: $file_count files"
exit $OK_STATE
Reply With Quote
  #3 (permalink)  
Old 01-25-10, 08:26
sant sant is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Hello

This script work ok now, sorry but I think I've had any problems with the copy&paste


thank you very much for your help
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On