Find the answer to your Linux question:
Results 1 to 9 of 9
Hello, I have a bash script which I'm trying to use to supervise free hard disk space, I downloaded it, it's called check_diskfree.sh. I'm running it on a proprietary linux ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8

    Lack Experience, help verifiy bash script


    Hello, I have a bash script which I'm trying to use to supervise free hard disk space, I downloaded it, it's called check_diskfree.sh. I'm running it on a proprietary linux system used for backing up data, it's by wooxo, so I don't have full admin rights (not sure if this is the problem).

    Anyway the linux version seems to be GNU/Debian, I've run the script on CentOS and it works fine ( but I also have full admin rights on that machine ).

    However in GNU/Debian I get the following error:

    Code:
     bash check_diskfree.sh /dev/sda
    check_diskfree.sh: line 15: syntax error near unexpected token `newline'
    'heck_diskfree.sh: line 15: `case $1 in
    I'm not a bash expert, but I really can't see where the problem is. Here is the code from the script, hope you can help.

    Code:
    #!/bin/bash
    # 
    # Plugin to check free disk space
    # using check_by_ssh
    # by Markus Walther 
    # The script needs a working check_by_ssh connection and needs to run on the client to check it
    # 
    # Command-Line for check_by_ssh
    # command_line    $USER1$/check_by_ssh -H $HOSTNAME$ -p $ARG1$ -C "$ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$"
    # 
    # Command-Line for service (example)
    # check_by_ssh!82!/nagios/check_diskfree.sh!hda1!75!90
    #
    ##########################################################
    case $1 in 
    --help | -h )
             echo "Usage: check_diskfree [dev] [warn] [crit]"
             echo " [warn] and [crit] as int"
             echo " Example: check_diskfree hda1 70 90"
             exit 3
    		 ;;
    	* )
    	;;
    esac
    
    if [ ! "$1" -o ! "$2" -o ! "$3" ]; then
            echo "Usage: check_diskfree [dev] [warn] [crit]"
            echo " [warn] and [crit] as int"
            echo " Example: check_diskfree hda1 70 90"
            echo "Unknown: Options missing"
            exit 3
    fi
    
    used=`df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f3`
    free=`df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`
    full=`echo $(($used+$free))`
    percent=`echo $((( $free * 100 ) / $full))`
    warn=`echo $((( $full * $2 ) / 100 ))`
    crit=`echo $((( $full * $3 ) / 100 ))`
    
    if [ "$warn" -gt "$crit" -o "$warn" -eq "$crit" ]; then
       echo "Unknown: [crit] must be larger than [warn]"
            exit 3
    fi
    
    if [ "$used" -lt "$warn" -o "$used" -eq "$warn" ]; then
            echo "OK. Free Space: `df -h /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
            exit 0
     elif [ "$used" -gt "$warn" -a "$used" -lt "$crit" ]; then
            echo "Warning. Free Space: `df -h /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
            exit 1
     elif [ "$used" -gt "$crit" ]; then
            echo "Critical. Free Space: `df -h /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
            exit 2
     else
       echo "Unknown"
       exit 3
    fi

  2. #2
    Just Joined!
    Join Date
    Jun 2012
    Posts
    5
    Hah, I recognize that output. You're gonna use this with Nagios.

    I successfully ran it as a non-sudoer on Ubuntu

    $ ./check_diskfree.sh sda 60 90
    OK. Free Space: 3.9GB, 99%

    I believe I found out your issue though!

    The issue is actually because of the way Linux/Unix handles line returns. If you save this file with proper linux newline escape sequences (\n) it works fine. I replicated your error by saving the file as Microsoft Notepad would (using gedit and specifying the "line ending" style as "Windows")

    I got curious and tested MacOS line return sequences (\r) too:

    $ ./check_diskfree.sh sda 60 90
    ./check_diskfree.sh: line 1: syntax error near unexpected token `('
    'i exit 3Unknown"tical. Free Space: `df -h /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \ -f4`B, $percent%"

    Copy paste the script into a text editor not made by Microsoft and save it again. It'll probably work.

    This should fix it too:
    Code:
    mv check_diskfree.sh check_diskfree.sh.bak ; sed 's/$'"/`echo \\\r`/" check_diskfree.sh.bak > check_diskfree.sh
    Last edited by keiran; 06-09-2012 at 02:38 AM.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    Script seems fine. Just you need to convert it to UNIX text file format.
    Using following command :

    Code:
    #dos2unix check_diskfree.sh
    output:

    dos2unix: converting file check_diskfree.sh to Unix format ...


    and you will be able to run that script again

    Thanks,
    Abhijeet

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8
    Well I tried all that and I still get the same error. That said, I don't have gedit on this server. Can't install it either. Don't have access to web on this server. Anyway I copied it across from windows via WinSCP and edited it with vi and saved it, that didn't work. And you commands dos2unix and the other one didn't work either.

    So what do I have to do to edit it manually? Just put \n at the end of every line?

  6. #5
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8
    I re-wrote the entire script by hand in VIM editor on the server, and it executes, however I now get permission errors related to the command df /dev/$1, the script doesn't seem to be able to have access to sda for example. Yet when I run df /dev/sda manually it works fine. I'm thinking because of this proprietary server, I can't login as root and therefore I can't give the script the rights it needs.

  7. #6
    Just Joined!
    Join Date
    Jun 2012
    Posts
    1
    This thread helped me. For future reference, try these commands in vim:
    :set ff?
    :set ff=unix
    :wq

    As for permissions, did you already try sudo?
    If that doesn't work, maybe you need to ask the system administrator to add you to sudoers.

  8. #7
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8
    Thanks for the commands. But I'll never get into the sudoers, it's a proprietary product, it's running it's own copyright software in debian, so I can't get full admin access. Plus already asked and they just try to sell you their own supervision crap products. Anyway thanks for the help guys.

    However, I realised that the df command works fine by itself, but when you try to affect it to a variable, that's when it gets angry about permissions. So I'm trying to put in all the commands in full, but it becomes a huge mess and I'm still trying to debug the syntax errors doh!

  9. #8
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8
    I just figured something out, now I'm no expert in bash but anyway:

    Instead of used=df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \ -f3

    Writing used=$(df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \ -f3)

    appears to get rid of the permission errors. However now it is complaining about the cut command.

    It says the delimiter must be a simple character and try cut --help,

    So I'm thinking it's a syntax error. Now I kind of know what the cut command is for, but what is he trying to do here with cut -d \ -f3 and why wouldn't it work?

  10. #9
    Just Joined!
    Join Date
    Jun 2012
    Posts
    8
    I finally found out by debugging this **** bit by bit that, the cut needs to be cut -d ' ' -f3 and not cut -d \ -f3. It's supposed to cut out to the 3 element in the list of data and skip to it by skipping "spaces", so christ knows why the guy put \ instead of ' ', or maybe I don't know much about bash. Anyway, also had to remove echo's from where they shouldn't have been. Here's the finished script:

    Code:
     #!/bin/bash
    
    case $1 in
    	--help | -h )
    		echo "Usage: check_diskfree [dev] [warn] [crit]"
    		echo " [warn] and [crit] as int"
    		echo " Example: check_diskfree hda1 70 90"
    		exit 3
    		;;
    	* )
    	;;
    esac
    
    if [ ! "$1" -o ! "$2" -o ! "$3" ]; then 
    		echo "Usage: check_diskfree [dev] [warn] [crit]"
    		echo " [warn] and [crit] as int"
    		echo " Example: check_diskfree hda1 70 90"
    		echo "Unknown: Options missing"
    		exit 3
    fi
    
    used=$(df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d ' ' -f3)
    free=$(df /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d ' ' -f4)
    full=$(($free+$used))
    percent=$((($free * 100 ) / $full))
    warn=$((( $full * $2 ) / 100)) 
    crit=$((( $full * $3 ) / 100))
    
    if [ "$warn" -gt "$crit" -o "$warn" -eq "$crit" ]; then
    	echo "Unknown: [crit] must be larger than [warn]"
    		exit 3
    fi
    
    if [ "$used" -lt "$warn" -o "$used" -eq "$warn" ]; then
    	echo "OK. Free Space:" $(df -h  /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d ' ' -f4)"B, $percent%"
    	exit 0
    
    elif [ "$used" -gt "$warn" -a "$used" -lt "$crit" ]; then
    	echo "Warning. Free Space:" $(df -h /dev/$1 | tail - n1 | sed -r 's/\ +/\ /g' | cut -d ' ' -f4)"B, $percent%"
    	exit 1
    elif [ "$used" -gt "$crit"  ]; then
    	echo "Critical. Free Space:" $(df -h /dev/$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d ' ' -f4)"B, $percent%"
    	exit 2
    else
    	echo "Unknown"
    	exit 3
    fi

Posting Permissions

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