Scripting/UNIX help

We may earn a small commission from affiliate links and paid advertisements. Terms

lswhitecivic

Senior Member
VIP
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.
 
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:
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
 
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. :)
 
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.
 
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.
 
Back
Top