[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Which tool, and how, to get partial string from file?



Miquel van Smoorenburg wrote:

In article <[🔎] 4127674A.9060802@acu.edu>,
Kent West  <westk@acu.edu> wrote:
On Fri, Aug 20, 2004 at 09:46:52PM -0500, Kent West wrote:
How can I test to see if the word "tuber" is in the /etc/passwd file, reliably, and take an action if it is, and take a different action if it's not, in a bash shell script?
How do I test to make sure the name is in two files? Like so?

At this point, I suggest you start studying shell script
programming. Get a book, or read one of the many online tutorials
such as http://www.tldp.org/LDP/abs/html/

Mike.

Mike, I appreciate your efforts to guide me along the way to becoming more proficient in solving my own programming needs. I'm probably over-reacting here, but I need to make a few points.

(1)
Your statement implies that I haven't attempted to read and study, but as I've stated elsewhere in this list:

I think I must have some mental block against understanding the syntax of "regular expressions" and/or "test" and/or "conditionals" and/or single-quotes-vs-double-vs-backtics and/or something. It seems every time I try to do some sort of scripting over the past several years (only 3 or 5 times a year, so not often), I have to google and "man" and study and ask, to essentially figure out the same sorts of things.

and

Because like I say above, I google around trying to find my own way before I come ask, and that just leaves me confused.


(2)
The online information did not address my needs (and I suspect that tree-bound versions would suffer similar shortcomings). Elsewhere in this thread I gave two examples of online scripting tutorials which increased my confusion rather than enlightening me. The particular tutorial you mention above would have cleared up one of my points of confusion ("<><>An *if* can test any command, not just conditions enclosed within brackets." - http://www.tldp.org/LDP/abs/html/testconstructs.html), but would have increased another point:

# The very useful "if-grep" construct:
# ----------------------------------- if grep -q Bash file
then echo "File contains at least one occurrence of Bash."
fi

as the version of grep in Woody (which I'm running) does not support the -q option.


(3)
What I was trying to accomplish, although presumably easy, turned out to be not so easy, as evidenced by the length of this thread before a satisfactory answer appeared. My point is that it turned out to require a programmer's mentality/experience to solve the problem. Unless your suggestion to "start studying shell script programming" has an implied ending of "become a proficient programmer", I'm not sure your statement isn't somewhat offensive, especially considering that I've already stated that I have tried to do my own homework.

Now, having reacted in a somewhat flamey way, I'll now calm down and state that I appreciate your intentions to help guide me in my journey toward becoming a proficient script programmer (even though that's not really my desired goal, but is a desirable and welcome outcome), and for providing me with a link that looks interesting.

I also want to thank everyone for attempting to help me on this off-topic thread; it's a testimony to the spirit of cooperation in the Debian community.

And I would like to say one final time: "Who would have thought it'd be so difficult to figure out how to test if a certain word was in a couple of text files?" Just for the record, here's what I settled on:

GREP=<path to a version of grep that understands -q>
if ( $GREP -q '\<tuber\>' /etc/passwd ) && ( $GREP -q '\<tuber\>' /etc/shadow )

then
# The files seem to be okay, so do stuff

else # The files are broken; send an email

echo "ERROR: tuber is missing from passwd or from shadow" | mutt -s "The files are muffed" admin@somewhere.com
  exit 1

fi

Notice I had to use a "rogue" grep to do the job.

--
Kent



Reply to: