The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 01-25-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
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

Producing:

Code:
% ./s1
Found it.

Make me feel at home:

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

Producing:

Code:
% ./s2
 Found it (expected).

The key to s2 is not the function, of course, it is the syntax of and in the if statement. The page at http://www.tldp.org/LDP/abs/html/index.html is long, but valuable... cheers, drl