How to test string for pattern

How to test string for pattern

Post by vict.. » Fri, 23 Jun 2000 04:00:00



Hi,
I wonder what the correct syntax is for testing of Korn shell string
variable for the pattern match. I need to test if string starts from
"#":
if [ "$var" == ^#* ] ; then ... ; fi

But that doesn't work. Why??
Many thanks...

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

How to test string for pattern

Post by Eric Amic » Fri, 23 Jun 2000 04:00:00



> Hi,
> I wonder what the correct syntax is for testing of Korn shell string
> variable for the pattern match. I need to test if string starts from
> "#":
> if [ "$var" == ^#* ] ; then ... ; fi
> But that doesn't work. Why??

Because you've got the wrong syntax.  It should be

if [[ "$var" = #* ]]; then ...

--
Eric Amick
Columbia, MD


 
 
 

How to test string for pattern

Post by Gus Schlachte » Fri, 23 Jun 2000 04:00:00



> Because you've got the wrong syntax.  It should be

> if [[ "$var" = #* ]]; then ...

Hmmm, that doesn't seem to work either. Try this:

   if expr "$var" : "#" > /dev/null; then ... fi

Or this:

  if [[ -z ${var%%#*} ]]; then ... fi

Gus

--------------------------------------------------

--------------------------------------------------

 
 
 

How to test string for pattern

Post by Eric Amic » Fri, 23 Jun 2000 04:00:00




>> Because you've got the wrong syntax.  It should be

>> if [[ "$var" = #* ]]; then ...

> Hmmm, that doesn't seem to work either.

Yeah, I forgot that # introduces comments.  On the other hand,

if [[ "$var" = \#* ]]; then ...

*will* work.  (I tried this one to be sure.)

--
Eric Amick
Columbia, MD

 
 
 

How to test string for pattern

Post by Ken Pizzi » Sat, 24 Jun 2000 04:00:00



>I wonder what the correct syntax is for testing of Korn shell string
>variable for the pattern match. I need to test if string starts from
>"#":
>if [ "$var" == ^#* ] ; then ... ; fi

case $var in
 \#*) ...
esac

Quote:>But that doesn't work. Why??

Because "==" in the test (aka "[") command is a string
comparison, not a pattern match.

                --Ken Pizzini

 
 
 

How to test string for pattern

Post by Dan Merc » Sat, 24 Jun 2000 04:00:00






>>> Because you've got the wrong syntax.  It should be

>>> if [[ "$var" = #* ]]; then ...

>> Hmmm, that doesn't seem to work either.

> Yeah, I forgot that # introduces comments.  On the other hand,

> if [[ "$var" = \#* ]]; then ...

> *will* work.  (I tried this one to be sure.)

Quotes are not needed around the LHS of a conditional expression:

   if [[ $var = \#* ]]; then ...

works for all values of var,  as does

   if [[ -f $var ]]

Quotes are reguired for some data on the RHS.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

1. Command to test a variable or string for patterns?

Hi, I'm writing my first shell script (bash shell) and I can't find a
command to do what I want.... The script succeeds at its purpose,
which is to rename a bunch of files at once, replacing an arbitrary
string with another arbitrary string.

The problem is I want to make it skip backup (~) files and I can't
find a way to test the variable $files to see if it contains the
tilde.  Is there a command that does such a thing? I can only find
commands like grep to search entire files, not variables or strings
..  And if this hypothetical command finds a tilde, how do I make it
then prevent mv from executing  for that filename?

My script:

#!/bin/bash
#A script to try out renaming a batch of files.
#PW's first-ever shell script!
echo "The value of the variable 1 is currently: $1"
echo "The value of the variable 2 is currently: $2"
echo "Replacing $1 with $2 in all filenames"
echo -n "in the directory " ; pwd
for files in *$1* ; do
    mv -i `(echo -n $files " " ; echo $files | sed s/$1/$2/)`
# The first arg to mv is thus $files ; the second is thus
# $files piped through sed to replace $1 with $2
done

==============================================================
Paul Winkler --- music & sound --- zarmzarm at hotmail dot com
                       a  member  of
ARMS --- an ongoing pop music experiment  --- our first album,
URBAN SUNDIAL  is now available.....on CD, vinyl, and cassette
from Label 12 13.........contact:     sonic113 at juno dot com

Also -- improvised & electronic music releases are coming real
soon, dirt cheap from plan B cassette works and label 12 13...
==============================================================

2. What means the link error "shdr table truncated"

3. test test test test test test test

4. How do I backup open files?

5. ksh conditional (string match pattern)

6. Why a 301 Moved Permanently ?

7. VI editor, replacement string pattern

8. File permission!

9. Matching a pattern in a file and inserting variable string above the line matched?

10. string pattern matching

11. How can I pattern-match a given string in ksh?

12. substitute string for pattern in KSH 88

13. Append string to each line if it matches a pattern