LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2014, 05:50 PM   #1
hugh.shinn
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
searching file for string with embedded space and dash


All -

I'm trying to use grep to find out if file in a ksh script contains the following string:

'spdsnsrv -listenport'

this string is contained in a ksh shell variable see code below:

spds1="'spdsnsrv -listenport'"

while [[ $count -le 3 ]]; do

spds_pid=spds"${count}"

eval grep \$${spds_pid} $PSFILE > /dev/null
...
((count=count+1))

done

I get the following results:

grep: invalid option -- t
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
grep: invalid max count



I have tried it with double quotes around the string, single quotes around the string and no quotes around the string.

How can I fix this?
 
Old 09-10-2014, 05:46 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Best not to use eval and indirect vars, too fragile; use an array to store the values instead eg http://wiki.bash-hackers.org/syntax/arrays
 
Old 09-10-2014, 06:17 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,777

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
you need to use fgrep (or grep -F), or grep -- 'spdsnsrv -listenport'
 
Old 09-10-2014, 12:36 PM   #4
hugh.shinn
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Pan64 et al -

I tried:

count=1

spds1="'spdsnsrv -listenport'"
spds2="'spdsserv -nameserverport'"
spds3="'spdssnet -listenport'"

echo spds1 = $spds1
echo spds2 = $spds2
echo spds3 = $spds3

########################################################################
# Get running Websphere process
########################################################################

ps -ef | grep spds > $PSFILE

while [[ $count -le 3 ]]; do

spds_pid=spds"${count}"

eval grep -- \$${spds_pid} $PSFILE > /dev/null

...
((count=count+1))
done

Here are the results:

spds1 = 'spdsnsrv -listenport'
spds2 = 'spdsserv -nameserverport'
spds3 = 'spdssnet -listenport'

grep: -listenport': No such file or directory
grep: -nameserverport': No such file or directory
grep: -listenport': No such file or directory


Any thoughts?
 
Old 09-11-2014, 01:43 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,777

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
I do not understand that line at all. What is the goal of it?
eval grep -- \$${spds_pid} $PSFILE > /dev/null
I think you do not need eval (eval is not a really safe command) and you need to use "" to protect your pattern: grep -- "pattern" file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Searching/replacing string for string Jalcock501 Programming 13 08-06-2013 03:20 AM
c programming :searching a sub string in a string batman4 Programming 2 02-06-2013 06:26 PM
Getting Segmention fault error while searching string in csv file saurabhmehan Linux - Newbie 9 01-19-2011 04:39 AM
Searching a string from the file amty Programming 6 11-06-2008 08:04 AM
awk searching a string from a file within another file changcheh Linux - Software 7 12-29-2006 10:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:01 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration