![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| compare string in two files | MiLKTea | Shell Programming and Scripting | 2 | 03-10-2008 05:35 AM |
| how to know if a string contains a certain pattern | Deanne | Shell Programming and Scripting | 11 | 08-09-2007 08:19 PM |
| String compare | sbasetty | Shell Programming and Scripting | 14 | 02-07-2007 05:24 AM |
| Compare Char to String | Phobos | High Level Programming | 3 | 04-09-2005 11:01 AM |
| string compare | gundu | Shell Programming and Scripting | 3 | 03-23-2005 04:42 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Compare string to a pattern
I am new to unix and need to learn how to compare a variable $subject to a string pattern. If the variable has the word "Item" in it then it should be true. How do I do this? Currently I am using the Bourne shell but I can also use Korn or Bash.
I come from a Rexx background where strings are easy: IF WORDPOS('Item', subject) > 0 THEN SAY 'Found it' END |
|
|||||
|
Hi.
Make you feel at home: Code:
#!/usr/bin/env rexx
/*
# @(#) s1 Demonstrate Linux rexx.
*/
subject = 'We are looking for an item in a line.'
If WORDPOS('item', subject) > 0 Then
SAY 'Found it.'
Else
SAY ' Cannot see item.'
exit 0
Code:
% ./s1 Found it. Code:
#!/bin/bash -
# @(#) s1 Demonstrate rexx function emulation.
debug="echo"
debug=":"
wordpos() {
local phrase="$1" string="$2"
$debug " wordpos, looking for $phrase in $string"
if [[ $string == *$phrase* ]]
then
return 0
else
return 1
fi
}
if wordpos item "Jack and Jill"
then
echo " Found it (unexpected!)."
fi
if wordpos item "Now here is an item embedded."
then
echo " Found it (expected)."
fi
exit 0
Code:
% ./s2 Found it (expected). |
| Sponsored Links | ||
|
|