New Unix user with shell script question using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New Unix user with shell script question using grep
# 1  
Old 05-18-2011
New Unix user with shell script question using grep

Hello,
I am a new Unix user and new to shell programming. I am working on a script to go through a log file and find the text error:
grep -i 'error' monplus.mplog
if I find the text error in the log file I would like to echo a message to the operator staing there is an error
I am currently using:
Code:
grep -i 'error' monplus.mplog || echo "no errors in file"

which is working fine
I would like to use a loop
Code:
if grep -i 'error' monplus.mplog
then
  echo "There is an error in the monplus.mplog file"
  echo "Please investigate"
else
  echo "There are no errors in the monplus.mplog file"
  exit 1
endif

which is not working and giving a syntax error

Any help would be appreciated...
Thank you...
...Dave

Last edited by Franklin52; 05-19-2011 at 03:30 AM.. Reason: Please use code tags
# 2  
Old 05-18-2011
You almost got it there. You can "and" the result with &&; its usage is pretty much the same as "||"

Code:
grep -iq 'error' monplus.mplog \
&& echo -e "There is an error in the monplus.mplog file\nPlease investigate" \
|| echo "There are no errors in the monplus.mplog file"

# 3  
Old 05-18-2011
One obvious error in the script is endif, shell has fi as end of if statement.
Also, see -q option for grep, it might be handy, as example see below:

Code:
if $(echo "ABCD"|grep -q -i "AB"); then echo OK; else echo NO;fi

would say OK

Code:
if $(echo "ABCD"|grep -q -i "XX"); then echo OK; else echo NO;fi

would say NO
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to set user password to never expire in UNIX servers

Hi, I have a requirement where in i need to write a shell script to set users password to never expire. I know the command which is used to perform the same .. which is chage command. But, could not figure out how to do the same in shell script. Could you please help me with the shell... (3 Replies)
Discussion started by: suren424
3 Replies

2. Shell Programming and Scripting

UNIX shell script question.

I need to check whether the directory is exist or not. only three letter will be passed as argument. from that it should pick the entire directory. Instead of banking and manfucuture the input will be passed as man or ban. $1 -> ban $2-> monday #!/bin/sh DIR='/sales/$1*/monday' if ;... (3 Replies)
Discussion started by: arun888
3 Replies

3. Shell Programming and Scripting

UNIX question on grep

Dear Sir, I need to remove the word /klp/ in all the files present in the directories can you tell me how to remove the word globally. grep "/klp/" * -exec ls -l Once the above command is exectubed I could see lot of files displayed. (8 Replies)
Discussion started by: ramkumar15
8 Replies

4. Shell Programming and Scripting

Unix Shell Script question

I have the following script ========= #!/bin/sh MUTEXPREFIX="/tmp/" READMUTEX=(test globallock) # If mutexes found - exit out for m in "${READMUTEX}"; do || (echo "$0 Mutex file found - Exiting\n" ; exit 1) done; echo "After for loop\n"; exit;============= What i want... (8 Replies)
Discussion started by: GosarJunk
8 Replies

5. Solaris

grep in a shell question

Hi Experts, I am able to run the below command via command line without any issues: cat test.txt | grep -A 2 "Title" However, when I put this same command on test.sh and do a ./test.sh, I get the error "grep: illegal option -- A Usage: grep -hblcnsviw pattern file . . ." Would... (3 Replies)
Discussion started by: marcusbrutus
3 Replies

6. Shell Programming and Scripting

Unix Shell scripting -How to skip User Standard input section from another script

All, problem Description: For example: I have two shell scripts(executables). let name it as script1 and script2.I'm trying to execute script1 from script2. while executing script2, script1 is asking for manual input(input from keyboard). Now i need to know how I can skip this user input... (3 Replies)
Discussion started by: techie99
3 Replies

7. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

8. Shell Programming and Scripting

Unix Shell Script Help - Grep

Hi, I am new to UNIX Shell scripting, and will require some help. I am trying to search a directory with a number of files for a specific word, then if that word is found in any of the files, then the filename is appended to a log file or result file by ">" command. I have been trying to... (2 Replies)
Discussion started by: Sypherg
2 Replies

9. Shell Programming and Scripting

Unix shell script (grep -A 6 -B 2 "ORA-" filename

BACKGROUND: I am using Solaris 10. Some of my boxes have gnu grep and I can use -A and -B flags on those. However, the solaris flavor of grep won't use the flags -A or -B. And some of my boxes won't be getting gnu grep. Should I try using perl, awk, or sed? Actual PROBLEM: I am... (7 Replies)
Discussion started by: el_guero
7 Replies

10. Shell Programming and Scripting

How to hide user inputted text for interactive unix shell script?

Hi everybody, Do you know how to hide the text for interactive unix shell script? Just like the case for inputting password during logon. Patrick (1 Reply)
Discussion started by: patrickpang
1 Replies
Login or Register to Ask a Question