Don't Quote Me On This...

Korn Shell Scripting on Solaris 9

Hello, this is my first post, I figure I share a problem and how I fixed it as well as ask a question that I'm currently stuck on. A version of "Give a Penny" "Take a Penny"

First Problem - I'm currently writing an automated version of one of the two types of Audit script for the company I work for (Myself and a coworker are split between AIX and SunOS. I have SunOS and he's doing AIX). We can do this manually, but why manually do these when you can have a script answer the questions for you in a quick and efficient manner? So I went to town and put together a nice script that worked for this first type of Audit. The issue was once I took it off of one SunOS server and onto another, I'd see issues arise. I therefore went back to the drawing board and am now in the process of redoing the scripts to "hopefully" be compliant throughout our company SunOS servers. In revising the script, I came across the following command in one of my functions.

The purpose of this function is to check the root's profile for 2 strings to be present. I have two different commands for the two different strings. The second command works fine trying to find the string "umask 027", the first string however, as you will see, has a few ticks, quotes, and double quotes to throw a nice wrench into the Unix gearworks. NOTE:There's a difference between the two strings in the egrep, look for the space after the ; before the "export".

I had the following command within my function:

rootpathvar1=`cat /.profile | egrep 'PS1="`/bin/hostname`#"; export PS1|PS1="`/bin/hostname`#";export PS1' | wc -l`

When the script ran, I wouldn't get any response (below this command I have an "if" statement having the ( number > 0 ) checked). I debugged and put an "echo $rootpathvar1" after the command and it echoed out a "0". Exiting the script and running the command on its own:

cat /.profile | egrep 'PS1="`/bin/hostname`#"; export PS1|PS1="`/bin/hostname`#";export PS1'

I received the output of:
PS1="`/bin/hostname`#";export PS1

First Problem's Answer:
If it worked there, I should have had a value of 1 in the script, so back to the drawing board I went. After looking it over, it became evident that the problem was with the "command ticks", aka the ` symbols:

rootpathvar1=`cat /.profile | egrep 'PS1="`/bin/hostname`#"; export PS1|PS1="`/bin/hostname`#";export PS1' | wc -l`

Even though I had the ' quotes in (which I gathered from resources to always give the literal meaning of what comes after it, my thinking being that everything after the ' to be a string until it came to the enclosing ') place, the ` outside the command and the ` inside the command clashed. I came across the power of \. It encloses the character after it as if it were its own quotes (please correct me if I'm wrong). So that command from above became:

rootpathvar1=`cat /.profile | egrep 'PS1="\`/bin/hostname\`#"; export PS1|PS1="\`/bin/hostname\`#";export PS1' | wc -l`

Running the script now produced the result I desired and I lived happily ever after....or so I thought.

Problem # 2, aka My Real Thorn In My Side
After that problem above, I was smooth sailing cruising through the rest of the script, editing and tweaking and fixing. I came to about 4 functions away from being done when I encountered the problem which is currently plaguing me. I am looking within the /var/spool/cron/crontabs/sys file for the following string:
0,20,40 * * * * /usr/lib/sa/sa1 (don't worry, my eyes rolled when seeing how I had to tackle this one as well)

I figured grep to the rescue and all would be good, I was wrong. I took this out to the command line to try and fix first.

If I enter the following command:

cat /var/spool/cron/crontabs/sys | grep "0,20,40 * * * * " (yes include the space after the last *)

Results = 0,20,40 * * * * /usr/lib/sa/sa1 (This is a good thing, but not my grep only contained everything up until the "/" character)

If I do the second half of the grep string:

cat /var/spool/cron/crontabs/sys | grep "/usr/lib/sa/sa1"

Results = 0,20,40 * * * * /usr/lib/sa/sa1 (Again, great, but I'm missing the first half of the grep string).

As soon as I put it all together:

cat /var/spool/cron/crontabs/sys | grep "0,20,40 * * * * /usr/lib/sa/sa1"

It shakes a finger at me and shows me no results, it doesn't work. I debugged and slowly but surely I started with doing the command, character by character.

cat /var/spool/cron/crontabs/sys | grep "0", then
cat /var/spool/cron/crontabs/sys | grep "0," so on and so forth until I get to

cat /var/spool/cron/crontabs/sys | grep "0,20,40 * * * * /"

Once the / is placed within the string, it no longer works. I've tried to switch the " with ' surrounding the grep string and that doesn't work. I've tried to put the \ character before it (i.e. "\/") to see if it takes it as a / character and not it's special meaning, but that doesn't work.

I have perused the Net and these forums as well to see if the question of this type of problem occuring, but to no avail. I know this was lengthy, but I hope in the process of answering this question, my Q&A at the beginning my help others working on strings and greps.

Thank you all for your time and patience in reading this short novella, and thank you for what help you can supply.

~Ryan

The asterisk is special in a RE and it will not match an asterisk unless it is backslashed.

$ echo "a * b" | grep "a * b"
$ echo "a * b" | grep "a \* b"
a * b
$

My hats off to you Perderabo, I thought I had tried that but I guess I didn't, putting your suggestion to use:

compserver#cat /var/spool/cron/crontabs/sys | grep "0,20,40 \* \* \* \* /usr/lib/sa/sa1"
0,20,40 * * * * /usr/lib/sa/sa1 <--- As my Admin mentor says, "Now you're cooking with gas!"

Thanks a lot!

Follow up:

Found some issues when trying to print out the variable after setting it. This is a note and reminder that when you go to echo the variable or print it, to enclose it in " ". Just so you all can see the finished product, here is the piece of the function:

filevar=`cat /var/spool/cron/crontabs/sys | grep '0,20,40 \* \* \* \* /usr/lib/sa/sa1'`
if [ "$filevar" = "0,20,40 * * * * /usr/lib/sa/sa1" ]
then
echo '0,20,40 * * * * /usr/lib/sa/sa1 Match In /var/spool/cron/crontabs/sys....PASS'
else
echo '0,20,40 * * * * /usr/lib/sa/sa1 NOT In /var/spool/cron/crontabs/sys......FAIL'
fi

Thanks again for everyone's help!
Ryan