Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    56

    Post Unanswered: assigning value to variable

    Hello ,

    I am using the following command to get the string from file

    error_txt=`grep "Not connected to Connectirect server" SCRIPT_LOG.log`

    my requirement is that if the error_txt has value "Not connected to Connectirect server " then has to send email has failed to connect if the error_txt has null value or empty then send an email successfully connnected .

    if [ -n $error_txt ]
    then
    echo " Failed to connect server "
    | mail -s " Status - Failed notice !! " $emailList
    fi
    if [ -z $error_txt ]
    then
    echo " connected to server successfully " | mail -s "
    transfer Status - Success !! " $emailList
    fi

    The above logic is not working for me ,. I am not sure why I am not getting email if the string error_txt has null value

    Pls advice .

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    You need to learn how to use code tags.
    Code:
    error_txt=`grep "Not connected to ConnectDirect server" SCRIPT_LOG.log`
    my requirement is that if the error_txt has value "Not connected to Connectirect server " then has to send email has failed to connect if the error_txt has null value or empty then send an email successfully connnected .

    Code:
    if [ -n $error_txt ]
    then
    echo " Failed to connect server " 
    | mail -s " Status - Failed notice !! " $emailList
    fi
    if [ -z $error_txt ]
    then
    echo " connected to server successfully " | mail -s " 
    transfer Status - Success !! " $emailList
    fi
    There is no way of knowing where the lines end. If your code is exactly as above, you need continuation punctuation.
    Code:
    if [ -n $error_txt ]
    then
    echo " Failed to connect server " \
    | mail -s " Status - Failed notice !! " $emailList
    fi
    if [ -z $error_txt ]
    then
    echo " connected to server successfully " | mail -s " \
    transfer Status - Success !! " $emailList
    fi
    Or is your script like this?
    Code:
    if [ -n $error_txt ]
    then
    echo " Failed to connect server " | mail -s " Status - Failed notice !!"$emailList
    fi
    if [ -z $error_txt ]
    then
    echo " connected to server successfully " | mail -s "transfer Status success !! " $emailList
    fi

  3. #3
    Join Date
    Nov 2009
    Posts
    56
    Yes my code is like what you said


    if [ -n $error_txt ]
    then
    echo " Failed to connect server " | mail -s " Status - Failed notice !!"$emailList
    fi
    if [ -z $error_txt ]
    then
    echo " connected to server successfully " | mail -s "transfer Status success !! " $emailList
    fi

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    I get a syntax error when $error_txt is null for the -z test, even enclosing $error_txt in quotes as:
    Code:
    if [ -z "$error_txt" ]
    Consider changing your code to:
    Code:
    error_txt=`grep -c "Not connected to ConnectDirect server" SCRIPT_LOG.log`
    if [  $error_txt = 0 ]
    then
        echo successful
    else
        echo failed
    fi

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Just query the return code:
    Code:
    error_txt=`grep "Not connected to Connect Direct server" SCRIPT_LOG.log`
    
    if [ $? -ne 0 ]
    then
      echo " Failed to connect server "| \
      mail -s " Status - Failed notice !! " $emailList
    else
      echo " connected to server successfully " | \
      mail -s "transfer Status - Success !! " $emailList
    fi
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  6. #6
    Join Date
    May 2014
    Posts
    6
    hi,

    unless you know why you don't, always write variables inside quotes :
    Code:
    if test -n "$var";...
    two reasons at least for this to be required:
    - if $var is empty, then test tests `-n' which is true !
    - if $var contains many words, then you'll get an error: `binary operator expected'

    if you don't need $error_txt further in your script
    Code:
    if grep -q 'pattern' file; then true; else false; fi
    you might want to store `$?' into another variable for later tests, otherwise testing `$?' is useless.

Posting Permissions

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