newb help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting newb help needed
# 1  
Old 03-31-2008
newb help needed

hi all, im working on a script that needs to read in a file, search for a string of text within that file, and if the string is found, do something. so i know i need to use an if-then statement, but how do i test for the existence of a string within the file? any help would be greatly appreciated.
# 2  
Old 03-31-2008
Try:

if $(grep -q PATTERN ${1})
# 3  
Old 03-31-2008
illegal option

here's my hack code:
-bash-3.00$ cat testfile | if $(grep -q Something ${1}); then echo "it's there"; fi
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .

looks like it doesnt like the q. im using solaris 10 here. i did a man grep and the q option does show up, im not sure why its failing here. i'm wanting to read in a file called testfile and look for the word "Something".
# 4  
Old 03-31-2008
Don't use the cat command in that context, do :
Code:
if $(grep -q Something testfile); then echo "it's there"; fi

A shortcut :
Code:
grep -q Something testfile && echo "it's there"


The -q option works fine for me under bash on my PC and ksh on mi AIX box.
In the both cases, the path of the command is /usr/bin/grep

The which command will show you the path of the grep command.
Code:
$ which grep
/usr/bin/grep
$

Try to specify the full path for the grep command.


Jean-Pierre.
# 5  
Old 03-31-2008
we have success!

hey, hey, hey! that first example worked just fine when i used the right version of grep! lol thanks SOOO much for your help!!
# 6  
Old 03-31-2008
The backticks -- aka $(command) -- are useless too, the following is quite sufficient.

Code:
if grep -q Something testfie; then ...

For the record, the -q option is quite trivial to replace; just redirect output to nowhere.

Code:
if grep Something testfile >/dev/null; then ...

# 7  
Old 03-31-2008
thanks

thanks for all the info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

newb at scripting how would I do this?

Hi guys. I have been lurking for quit some time attempting to learn on my own but I have reached an impasse. I need to extract specific details from from a text file that I get and parse it out into separate files with the name of each individual and the date it was proccess. Then within... (2 Replies)
Discussion started by: lostpacket
2 Replies

2. HP-UX

Newb Help, need to image one HD to another

Hello, I am totally new to Unix We have a piece of test equipment, three of them actually, that run HP-UX 9.0 from like 1994. We had backup tapes with procedures to load the OS and our test software, but the 16-track tapes are corrupt now. HP will not support it, they dont have it anymore,... (11 Replies)
Discussion started by: Newball80
11 Replies

3. Shell Programming and Scripting

Newb with While loop question

My first post here.... I have a few years exp with linux distros and some very basic Python..Ive been intent on learning shell scripting the last few weeks. Please excuse my crude efforts. I am running a program that takes network data containing US city names in plain text. I am TRYING to... (7 Replies)
Discussion started by: dddkkk
7 Replies

4. Shell Programming and Scripting

Help a newb with sed?

So here is my problem: I need to do a sed search and replace on the following: variable_blah_STATUS replaced with "replace string". sed 's/variable_blah_STATUS/replace string/g' works great but there is a catch. The catch is that I need to check and not replace variable_blah_STATUS... (3 Replies)
Discussion started by: phreezr
3 Replies

5. UNIX for Dummies Questions & Answers

Newb here....Someone spying on me?

Hey all. I'm brand spankin' new here to the board. I'm also a novice at Unix. I've been using it for about 1 year now but I haven't really expanded my abilities since I learned the basics. I am in school and I mainly just use Unix to submit stuff to assignment directories and use it to compile my... (3 Replies)
Discussion started by: andre2000
3 Replies

6. UNIX for Dummies Questions & Answers

Ok I'm a Newb, Please Help

I am wanting to download the Linux Program. When i go to download it I see several things to download. What do i download exactly? Then what do I do to install it. Also I have partitioned my hard drive to make way for Linux on the other part to play with it, will any problems arise from this? I... (3 Replies)
Discussion started by: Seeto
3 Replies

7. UNIX for Dummies Questions & Answers

Newb question

I don't even know if this should go here but I just would like to know what this means: d0e45878043844ffc41aac437e86b602 I know absolutely nothin' about UNIX, and I found this in a SQL table in a board I run. Someone please tell me what that is in "normal" mode. Pardon me for my... (4 Replies)
Discussion started by: daeglin
4 Replies

8. UNIX for Dummies Questions & Answers

help for a newb

ok i just installed FreeBSD 4.8 on a computer i had lying around and it was working ok but then when i tried to set up KDE's xdm (kdm) i think i must have configured the login manager wrong because i cant login through the kdm and therefore i can log in to BSD at all?! please can someone help me... (5 Replies)
Discussion started by: h3x
5 Replies

9. UNIX for Dummies Questions & Answers

100% newb - please help

I run windows 2000 right now. I want to learn how to use unix/linux. How do i go about installing Unix/Linux? What's the easiest version to install? Where can i get it? Is my Hardware Compatable? How do i partition my Hd? HELP!!!!!:confused: (6 Replies)
Discussion started by: Rawdawg
6 Replies

10. UNIX for Dummies Questions & Answers

The biggest newb ever...

Hi, all you unix people. I am a pretty advanced windows user, but I am curious about unix. Is there any reason I should attempt to acquire some form of unix for my home computer system? What sort of things is unix useful for? Unix is open source, right? Assuming that to be the case, I infer that... (3 Replies)
Discussion started by: BoneMalone
3 Replies
Login or Register to Ask a Question