Don't Quote Me On This...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Don't Quote Me On This...
# 1  
Old 11-01-2006
Lightbulb 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
# 2  
Old 11-01-2006
The asterisk is special in a RE and it will not match an asterisk unless it is backslashed.

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

# 3  
Old 11-01-2006
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!
# 4  
Old 11-01-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. What is on Your Mind?

Quote of the day

The authors of that are in charge of a $250M public project. (7 Replies)
Discussion started by: jgt
7 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replacing Double Quote in Double Quote incsv file

Hi All , We have source data file as csv file and since data could contain commas ,each attribute is quoted into double quotes.However problem is that some of the attributa data also contain double quotes which is converted to double double quote while creating csv file XLs data : ... (2 Replies)
Discussion started by: Shalini Badal
2 Replies

4. UNIX for Dummies Questions & Answers

To quote or not

My question is, "Do I not understand, or is my information out of date?" I am trying to just be a student, rtfm'ing. I am working on my work systems. Is it simply that the book was printed in 2002 and a lot has changed since then, or did I miss something? Working in Korn Shell. I have been... (5 Replies)
Discussion started by: g.j.huebschman
5 Replies

5. Shell Programming and Scripting

replacing a quote in some lines with multiple quote fields

i want to replace mistaken quotes in line starting with tag 300 and relocate the quote in the correct position so the input is 223;25 224;20100428064823;1;0;0;0;0;0;0;0;8;1;3;9697;18744;;;;;;;;;;;; 300;X;Event:... (3 Replies)
Discussion started by: wradwan
3 Replies

6. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

7. Shell Programming and Scripting

double-quote inside double-quote

hey all, i made a simple .sh like this: echo "<style media="screen" type="text/css">@import url("main.css");</style>" but the output is: <style media=screen type=text/css>@import url(main.css);</style> i want to keep double-quotes, can anyone help me? thanks (3 Replies)
Discussion started by: indraf
3 Replies

8. Shell Programming and Scripting

Capturing Data between first quote and next quote

I have input file like RDBMS FALIURE UTY8703 'USER_WORK.TEST' .HIghest return code '12' I want to parse data which comed between first quote till next quote USER_WORK.TEST can you please suggest how to do that (4 Replies)
Discussion started by: scorp_rahul23
4 Replies
Login or Register to Ask a Question