🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #1422

Re: Csh: first character of an arg is '-': Solution

#1422
From: Tom Christiansen
Date: Sun, 07 Oct 1990 15:22
66 lines
2996 bytes
In article <JAK.90Oct7001815@bimini.cs.brown.edu> jak@cs.brown.edu writes:
>Before I get flooded with more replies, let me post the answer that
>barnett@crdgw1.ge.com, raymond@math.berkeley.edu and
>christos@theory.tn.cornell.edu all basically gave me:

>foreach i ($*)
>  if ( $i =~ -* ) then
>    # is an arg
>  else
>    # is not
>  endif
>end

>Apart from the typo ~= instead of =~ , my problem was that I was quoting
>the right hand side, thinking that I did not want filename expansion,
>though as Raymond pointed out, I *do* want filename expansion, but
>expansion in the context of the left hand side, not of the current
>directory.  It seems like strange magic to me, but it works...

Yes, it is strange magic; welcome to the csh.  This solution won't work 
if one of the arguments is a built-in test, like -x.  Try it.  Csh
claims that it is missing a file name.   If you write it this way:
	
	if ( x$i =~ x-* ) then

you get around that problem.

>So thank-you for the replies.  By the way, no-one yet has answered my
>second question: can you extract a character from a word?  (Again, no
>forking allowed).

Hm... in Bourne or csh but without forking???  That's a pretty tough order.

I'm not a power ksh user, but I did cobble something together.  In ksh,
you should be able to hack off the end of the variable (call it $x).  You
use the ${x%pat} syntax.  The problem is you don't know the pattern, which
would be a bunch of ?'s, as many as one less than the length of the
string.  I managed to do it one at a time until small enough:

    y='foobar'
    x="$y"
    while [ ${#x} -ne 1 ]; do x=${x%?}; done
    echo y is $y, x is $x

This shouldn't take any forking, but is pretty lame I must admit.  But
what do you expect for a command interpreter trying to pass itself off as
a programming language? :-(  It really needs a ${var#s/a/b/#} with real
regexps, so I can get some reasonable back-referencing (you know, \1 stuff).  
Again, there may be better ways that I don't know in ksh, but I did scan
the man page.

If you'd chosen to write your script in perl, you could have said
something like any of these:
    
    $x=substr($y,0,1);		
or
    ($x) = ($y =~ /^(.)/);
or
    ($x = $y) =~ s/^(.).*/$1/;

Again, no forks involved.

--tom
--
 "UNIX was never designed to keep people from doing stupid things, because 
  that policy would also keep them from doing clever things." [Doug Gwyn]
Technical Details
Message-ID: <106932@convex.convex.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!snorkelwacker!usc!cs.utexas.edu!natinst!bigtex!texsun!convex!convex.COM
References: <JAK.90Oct7001815@bimini.cs.brown.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!snorkelwacker!usc!cs.utexas.edu!natinst!bigtex!texsun!convex!convex.COM
From: tchrist@convex.COM (Tom Christiansen)
Newsgroups: comp.unix.shell
Subject: Re: Csh: first character of an arg is '-': Solution
Message-ID: <106932@convex.convex.com>
Date: 7 Oct 90 15:22:20 GMT
References: <JAK.90Oct7001815@bimini.cs.brown.edu>
Sender: usenet@convex.com
Reply-To: tchrist@convex.COM (Tom Christiansen)
Distribution: comp
Organization: CONVEX Software Development, Richardson, TX
Lines: 66