1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Scripting/UNIX help

Discussion in 'Members' Lounge' started by lswhitecivic, Jun 14, 2013.

  1. lswhitecivic

    lswhitecivic Senior Member

    Messages:
    774
    Likes Received:
    37
    Joined:
    Aug 4, 2003
    Location:
    DFW, TX
    I know there are quite a few IT guys here, so I'm sure some have more experience working in UNIX than I do.

    Here is my problem:

    Once a week I have a cronjob that runs to check the indexes on our Unidata database for corruption.

    I didn't build this script, I just know what it does.

    When the job runs, it creates a file in /tmp called index.err.

    # more index.err
    PHANTOM process 28579 started.
    COMO file is '_PH_/root23402_28579'.

    Inside that file, I get the name of the actual phantom process that runs. If I browse to _PH_ directory and view the root23402_28579 file that has all the information I need to see related to the index checker.

    I'd like to create a cronjob that will email me the contents of that file. I can easily use a mailx command to send the email, but the problem is that file name will change every week.

    Do any of you know how I can extract the file name in a variable to then call when I do my mailx command?

    Hopefully that all makes sense. Let me know if you have any questions.
     
  2. lswhitecivic

    lswhitecivic Senior Member

    Messages:
    774
    Likes Received:
    37
    Joined:
    Aug 4, 2003
    Location:
    DFW, TX
    I'm thinking I'll need to do something like this:

    cat /tmp/index.err | grep root

    That gets me the line I need, but I don't know how to capture only the file name in a variable.

    This is probably a super easy script for someone well versed in UNIX, but I unfortunately am not.
     
    Last edited: Jun 14, 2013
  3. reikoshea

    reikoshea HS Troll...And Mod Moderator VIP

    Messages:
    12,633
    Likes Received:
    193
    Joined:
    Apr 27, 2005
    Location:
    San Antonio, TX
    filename=`grep 'COMO file' index.err | cut -f2 -d\'`

    Will return _PH_/root23402_28579

    If you dont want the directory:

    filename=`grep 'COMO file' index.err | cut -f2 -d\' | cut -f2 -d/`

    if you're not familiar with cut, it will set a delimiter (-d) and then the -f will set which field you want.

    for example if you've got a line that is "/mnt/media/debian" and you wanted to cut off the "/mnt/" section you could do this:

    echo "/mnt/media/debian" | cut -f2,3 -d/

    Hope that helps
     
  4. lswhitecivic

    lswhitecivic Senior Member

    Messages:
    774
    Likes Received:
    37
    Joined:
    Aug 4, 2003
    Location:
    DFW, TX
    Awesome! That worked perfectly, thanks man!

    Here is my completed script.

    #
    #This script sends the weekly index check log file to be reviewed.
    #
    #!/usr/bin/ksh

    PATH=$PATH:/opt/perl/bin:/usr/contrib/bin
    export PATH=$PATH

    CREATEDATE=$(date +%x)
    filename=`grep 'COMO file' /tmp/index.err | cut -f2 -d\' | cut -f2 -d/`

    cat /sb/SB.EXC/_PH_/$filename | mailx -s "Index Check for ${CREATEDATE}" *email address*

    I knew it had to be easy for someone with more UNIX experience than me. :)
     
  5. reikoshea

    reikoshea HS Troll...And Mod Moderator VIP

    Messages:
    12,633
    Likes Received:
    193
    Joined:
    Apr 27, 2005
    Location:
    San Antonio, TX
    you might want to start the script with this:

    Code:
    if [[ -r /tmp/index.err ]]; then
        exit 0
    fi
    
    That way you dont get a blank email or one that just says file doesn't exit. Just does a clean exit if /tmp/index.err is not readable or doesn't exist.
     
  6. reikoshea

    reikoshea HS Troll...And Mod Moderator VIP

    Messages:
    12,633
    Likes Received:
    193
    Joined:
    Apr 27, 2005
    Location:
    San Antonio, TX
    you might want to start the script with this:

    Code:
    if [[ -r /tmp/index.err ]]; then
        exit 0
    fi
    
    That way you dont get a blank email or one that just says file doesn't exit. Just does a clean exit if /tmp/index.err is not readable or doesn't exist.
     
Verification:
Draft saved Draft deleted

Share This Page