Find the answer to your Linux question:
Results 1 to 2 of 2
Well first of all I should say I'm new to the forums, and bash scripting in general. So I'm trying to create a small simple script that's goal is to ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5

    Small if statement | Help

    Well first of all I should say I'm new to the forums, and bash scripting in general.

    So I'm trying to create a small simple script that's goal is to send an email using the mail command and redirect in a file. I created a simple one with an if statement that pings gmail's ip and then greps for a TTL, and if successful, it executes the mail command.

    But I'm trying to put more error troubleshooting logic into it, since it's scheduled to run in the middle of the night. The idea is to ping gmails ip, and if that works then go mail. And if that fails, I want it to go and ping my ISP's dns and if that's successful, to mail it ... and then use `echo "Mail error - GMail IP failed - Message sent" /home/user/Desktop/Error`. And if that second ping to the ISP DNS fails, for the script to ping my default gateway, and regardless if it's contactable or not to append into that file, with the success or failure of the gateway ping and then exit.

    I apologize if this is unclear, or just bad logic in general ... but this is what i've come up with thus far.

    ---------------------------------------------------------------------
    1 #!/bin/bash
    2
    3 location1=64\.233\.163\.111
    4 location2=24\.220\.0\.10
    5 location3=192\.168\.0\.254
    6
    7 success1=`ping -c 1 $location1 | grep ttl`
    8 success2=`ping -c 1 $location2 | grep ttl`
    9 success3=`ping -c 1 $location3 | grep ttl`
    10
    11 if [ -z "$success1" ]; then
    12
    13 if [ -z "$success2" ]; then
    14
    15 if [ -z "$success3" ]; then
    16 echo "Gateway down"
    17 else echo "Gateway works"
    18
    19 else echo "ISP DNS replies"
    20 fi
    21
    22 else echo "Send email code here."
    23 fi

    ---------------------------------------------------------------------

    And the output of this is

    ---------------------------------------------------------------------
    ./testpings: line 19: syntax error near unexpected token `else'
    ./testpings: line 19: ` else echo "ISP DNS replies"'
    ---------------------------------------------------------------------


    Any help is greatly appreciated.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You're missing a 'fi' after line 17.

Posting Permissions

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