LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2012, 08:47 PM   #1
redhatwannabe
Member
 
Registered: Dec 2008
Posts: 83

Rep: Reputation: 15
how to find out if string exists in file


currently my code

Code:
if [ z `grep \"# listener.ora Network Configuration File\" $ORACLE_HOME/network/admin/listener.ora` ]
  then
        echo "listener.ora exists but all listeners are removed"

      cp $DB_CREATE_PATH/tmp_listener.ora ${ORACLE_HOME}/network/admin/listener.ora
      STARTLSNR=true

  fi
I wanted to detect string "listener.ora Network Configuration File" in $ORACLE_HOME/network/admin/listener.ora, if there's such a string,

copy tmp_listener.ora into listener.ora and set STARTLSNR to true

how should I achieve this? I'm using bash and current using RHEL 5.3

thanks

thanks
 
Old 11-11-2012, 09:19 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
grep is the standard way of looking for strings in a file. Typically in a script grep's default behaviour of displaying matching lines is not wanted and grep's -q (a.k.a --quiet or --silent) option is used to suppress non-error output. The return code from grep is used to determine whether the string was present in the file.

For example
Code:
c@CW9:/tmp$ echo foo > input.txt
c@CW9:/tmp$ grep --quiet foo input.txt
c@CW9:/tmp$ echo $?
0
c@CW9:/tmp$ grep --quiet bar input.txt
c@CW9:/tmp$ echo $?
1
 
Old 11-13-2012, 04:20 PM   #3
DutchGeek
Member
 
Registered: Sep 2006
Distribution: SuSE, Slackware
Posts: 55

Rep: Reputation: 10
you can do like catkin suggested, but an even simpler way without if statement:

Code:
 grep --quiet " listener.ora Network Configuration File" $ORACLE_HOME/network/admin/listener.ora || echo "listener.ora exists but all listeners are removed"
The logical OR will execute second statement only when the first returns nonzero.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot find certain .so file, but it *exists* in /usr/local/lib/ Kreshna Linux - Software 4 11-19-2009 12:05 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
SH Prg: Checking if a string exists in a file ignignokt Programming 6 12-14-2006 01:31 PM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration