test if string contains another string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers test if string contains another string
# 1  
Old 01-31-2009
test if string contains another string

I have already received one answer a part of my problem and I thought I could move on without help from there but I am failing miserably.

I am using a source control system called mercurial which, from /bin/sh script I can do this:

Code:
requiredText="This file has been modified"
hgLogOutput=`hg log -v -r $HG_NODE`
filePaths="${hgLogOutput#*files: }" filePaths="${filePaths%description:*}"

declare -a Array=($filePaths)
for filePath in $filePaths; do
    requiredText=hg cat $filePath | grep -F $requiredText
done

You may be able to see that my code fails on the:

Code:
requiredText=hg cat $filePath | grep -F $requiredText

I am trying to check if:

Code:
hg cat $filePath

contains the:

Code:
$requiredText

shell programming has to be the most difficult language I can imagine. Any help appreciated; it has taken me a day to write that which I have so far - and even then with help!

Last edited by klogger; 01-31-2009 at 09:18 PM..
# 2  
Old 02-01-2009
I managed to get it going:

Code:
requiredTextResult=`hg cat -r tip $filePath | grep -F "$requiredText"`
if [[ ! $requiredTextResult =~ $requiredText ]]; then
    # do stuff
    exit 1
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To test whether a field contains string value

I want to find whether string values are available in a field of a file. Let it be any character other than number . I just want to know whether characters will be available in it . Please share a single step command for this without creating a shell script. (5 Replies)
Discussion started by: Sang
5 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

test for a blank string

Hi guys I am performing a simple test for a blank string with the following code: if ] ]] ; then echo "Blanks are NOT a valid input " return 1 fi The above fails giving a syntax error message: syntax error at line 142 : `=~' unexpected I am in ksh88 is there... (2 Replies)
Discussion started by: aoussenko
2 Replies

4. Shell Programming and Scripting

string test

How do I use bash to test if a line begins with a random number of spaces followed by a letter? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Test if string exists

How do I use bash to test whether a string occurs more than two times in a file? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

string test in IF statement

How do I test multiple words in a string test like below: if ] then print "You entered $TBS name.\n" else print "You entered an incorrect response.\n" fi This test does not work. I have tried different syntax versions. How does this work? And is there a better way to do it? ... (10 Replies)
Discussion started by: djehresmann
10 Replies

7. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

8. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

9. UNIX for Dummies Questions & Answers

test a string...

Hi! I'm using echo $string | grep "" -c to test in a script if a string is a number and it seems to work. But how can i find, for example, if a string is a four figures number ? Thanks to all! (2 Replies)
Discussion started by: Kaminski
2 Replies

10. UNIX for Dummies Questions & Answers

string test?

hey guys- what is the syntax for a string test to verify that a user has entered a 6 digit numeric? Thanks for your help in advance! Todd (9 Replies)
Discussion started by: hedrict
9 Replies
Login or Register to Ask a Question