Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    23

    Unanswered: 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

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  3. #3
    Join Date
    Sep 2003
    Posts
    23
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •