问题描述:

I have a little problem with my bash script

i got a school project where i have to make a bash script to check if the host is up every 5 minuttes and if fails send email

i had problems with the "fi" statement but fixed the error.

now when i run the script i get an error: line 17 to many arguments"

it initiate the ping command (my Anti virus is blocking the ICMP, so i know the ping lines work)

#!/bin/bash

#Server-status script

FAILS=0

EMAIL_ADDRESS="[email protected]" ##Email capabilities

SERVER="192.168.1.1" ###Host to check

SLEEP=300 ###Defining Sleep

while true; do

ping -c 1 $SERVER >/dev/null 2>&1

if [ $? -ne 0 ] ; then #if ping exits nonzero...

FAILS=$"[FAILS + 1]"

else

FAILS=0

fi

if [ $FAILS -gt 4 ]; then

FAILS=0

echo "Server $SERVER is offline!" \

| mail -s "Server offline" "$EMAIL_ADDRESS"

fi

sleep $SLEEP #check again in SLEEP seconds

done

网友答案:

use declare -i to use FAILS as integer and initialize to 0

declare -i FAILS=0

then sum 1

FAILS=$FAILS+1 

here is my code(I've commented the mail commmand):

#!/bin/bash

#Server-status script

declare -i FAILS=0
EMAIL_ADDRESS="[email protected]" ##Email capabilities 
SERVER="192.168.1.1"  ###Host to check
SLEEP=1           ###Defining Sleep

echo "1-FAILS[$FAILS]"

while true; do
   ping -c 1 $SERVER >/dev/null 2>&1
   if [ $? -ne 0 ] ; then #if ping exits nonzero...
       FAILS=$FAILS+1 
   else
       FAILS=0
   fi

   echo "2-FAILS[$FAILS]"

   if [ $FAILS -gt 1 ]; then
       FAILS=0
       echo "Server $SERVER is offline!" # \ | mail -s "Server offline"   "$EMAIL_ADDRESS"
   fi
       sleep $SLEEP #check again in SLEEP seconds
done

output:

sh-4.3$ bash -f main.sh                                                                                                                                                                                                                                
  1-FAILS[0]                                                                                                                                                                                                                                             
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]  

I hope this can help

Regards Claudio

网友答案:

You are expanding the $FAILS variable with content [FAILS + 1], getting an invalid [] syntax.

Change FAILS=$"[FAILS + 1]" to:

FAILS=$((FAILS+1))
相关阅读:
Top