Using csh's =~ operator to match partial strings.

Using csh's =~ operator to match partial strings.

Post by Jamshid Afsh » Wed, 09 Nov 1994 14:07:28



I'm trying to use the =~ operator in my .cshrc so that I can check
whether I'm running SunOS 4.1.3 or Solaris 5.x.  `uname` on both
systems returns "SunOS" while `uname -r` returns strings like
"4.1.3_U1" or "5.2".

I would prefer to use the csh =~ operator instead of a more verbose
switch statement.

        if (`uname -r` =~ "4*") then
        set path = (~/local/sunos.bin $path)
        else
        set path = (~/local/solaris.bin $path)
        endif

Unfortunately the test always fails.  Why doesn't "4*"  match
"4.1.3"?  The csh man page says:

          ==   !=   =~   !~   equal to, not equal  to,  filename-
                              substitution      pattern     match
                              (described    below),     filename-
                              substitution pattern mismatch
     [...]
     The operators: ==, !=, =~, and !~ compare their arguments as
     strings;  other operators use numbers.  The operators =~ and
     !~ each check whether or not a string to the left matches  a
     filename  substitution  pattern  on the right.  This reduces
     the need for switch statements when pattern-matching between
     strings is all that is required.

Thanks,
Jamshid Afshar

 
 
 

Using csh's =~ operator to match partial strings.

Post by Tom Park » Thu, 10 Nov 1994 10:48:14


: I'm trying to use the =~ operator in my .cshrc so that I can check
: whether I'm running SunOS 4.1.3 or Solaris 5.x.  `uname` on both
: systems returns "SunOS" while `uname -r` returns strings like
: "4.1.3_U1" or "5.2".

: I would prefer to use the csh =~ operator instead of a more verbose
: switch statement.

:       if (`uname -r` =~ "4*") then
:       set path = (~/local/sunos.bin $path)
:       else
:       set path = (~/local/solaris.bin $path)
:       endif

: Unfortunately the test always fails.  Why doesn't "4*"  match
: "4.1.3"?  The csh man page says:

Try taking out the double quotes, like this:

        if (`uname -r` =~ 4*) then

That worked when I tried it.

-- Tom

 
 
 

Using csh's =~ operator to match partial strings.

Post by Jamshid Afsh » Fri, 11 Nov 1994 01:39:27



>[...]
>I would prefer to use the csh =~ operator instead of a more verbose
>switch statement.

>    if (`uname -r` =~ "4*") then
>    set path = (~/local/sunos.bin $path)
>    else
>    set path = (~/local/solaris.bin $path)
>    endif

>Unfortunately the test always fails.  Why doesn't "4*"  match
>"4.1.3"?  The csh man page says:
>     [...] The operators =~ and
>     !~ each check whether or not a string to the left matches  a
>     filename  substitution  pattern  on the right.  This reduces
>     the need for switch statements when pattern-matching between
>     strings is all that is required.

Thanks to all who e-mailed or posted the answer.  It turns out that I
should NOT be doing ANY quoting the filename substitution pattern on
the right.

        if (`uname -r` =~ 4*) then
        echo This is SunOS 4.x.
        endif

I would never have guessed that because using an unqoted asterisk in
other equality tests causes an attempt to match a file in the current
directory:

        if ($FOO == foo*) then  # causes csh error if foo* doesn't match a file
        ...

Jamshid Afshar

 
 
 

Using csh's =~ operator to match partial strings.

Post by Thomas Michanek - Michag » Fri, 11 Nov 1994 09:47:49


 > I'm trying to use the =~ operator in my .cshrc ...

 >   if (`uname -r` =~ "4*") then

 > Unfortunately the test always fails.  Why doesn't "4*"  match
 > "4.1.3"?  The csh man page says:

 >      The operators: ==, !=, =~, and !~ compare their arguments as
 >      strings;  other operators use numbers.  The operators =~ and
 >      !~ each check whether or not a string to the left matches  a
 >      filename  substitution  pattern  on the right.  This reduces
 >      the need for switch statements when pattern-matching between
 >      strings is all that is required.

The man page is perfectly correct. However, filename substitution
patterns cannot be quoted if they are to work as expected. Try:

        if (`uname -r` =~ 4*) then

If you don't see the point, try ls "*" at your csh prompt...

--
   _______   __  __ _     _                       ,------- Michagon -------.
  / _____|\ |  \/  (_)___| |__ ___ __ _ ___ _ __  |   (Thomas Michanek)    |
 / <|___  \|| .  . | / __) '. `-_ / _` / _ \ '. | |  Trumslagaregatan 118  |
|\_____|> / |_|`'|_|_\___)_||_(_,_\__, \___/_||_| |S-58346 Linkoping SWEDEN|
 \|______/  |_____________________|___/_________| |+46 13 273727(voice/fax)|

 
 
 

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

Hello all,  here is a snipet of code for a problem i'm having.  I've
tried to use sed for this, but i cannot get the variable substitution
worked out.

print "Please enter each username(s) you wish to add: "      
read NAMES
for USER in $NAMES ; do
  if egrep "^$USER" /etc/passwd > /dev/null ; then
       ### insert sed, awk, or perl code here
  fi
done

### The layout of the file is:
root ADMIN=ALL JBP=ALL
* ADMIN=JBP JBP=ENDUSER+BU+ARC

For each $USER, I need to add the following string ABOVE
the "* ADMIN=JBP" line in the file:  $USER ADMIN=ALL JBP=ALL

Thanks in advance for any help.

jim

2. LP problem

3. match works, don't match doesn't

4. does O_NDELAY toggle when set with fcntl()?

5. regexp to match lines that don't contain string?

6. X modelines for Samsung 700IFT

7. db partial matching

8. Better output from write

9. remove matching partial lines from two files ...

10. Invoking shell function, shell script or alias with only partial match?

11. A bug or an idiot: NCSA1.5 right to left partial matching of group names?

12. The %% operator for matching in shell scripts

13. Question: Ksh pattern matching operators