[comp.unix.shell] csh question

calvin@sequent.sequent.com (Calvin Goodrich) (11/08/90)

can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
stand for in csh? i've seen these used before but couldn't figure them out.
for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
get an informative answer.

next question: do these things have an equivalent in ksh? apparently ksh
doesn't have these little buggers. if they're useful i want to be able to
use them in my favorite (imho, anyway) shell.

first person to give me a good answer to both questions gets a free cup of
coffee at my company's cafeteria.  :]


thanx guys,

calvin.

minor@motcid.UUCP (Kevin E. Minor (+1 708 632 7043)) (11/08/90)

calvin@sequent.sequent.com (Calvin Goodrich) writes:

|>can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
|>stand for in csh? i've seen these used before but couldn't figure them out.
|>for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
|>get an informative answer.

OK this is out of my little UNIX pocket handbook back when I was at school
so if it's out of date or something... Sorry.

Argument modifiers (follow argument selectors):
:h	Leave only the head of a pathname.
:r	Remove the extension of a pathname, leaving the root.
:e	Leave only the extension part of a pathname.
:t	Leave the tail of a pathname, removing leading components.
	(My favorite since I use it in my prompt - i.e. $cwd:t)
:g[shret&]	Repeat action of modifier globally.
:q	Quote substituted words to prevent further substitution.
:x	Like (q), but break into words at blanks, tabs, newlines.

And here are the remainder modifiers - even though you didn't ask for them.
:s/<l>/<r>/	Substitute string <r> for string <l>.
:&	Repeat previous expression.
:p	Print but do not execute.

|>next question: do these things have an equivalent in ksh? apparently ksh
|>doesn't have these little buggers. if they're useful i want to be able to
|>use them in my favorite (imho, anyway) shell.

Do know, I try to stay away from ksh... (Just my preference...)

|>first person to give me a good answer to both questions gets a free cup of
|>coffee at my company's cafeteria.  :]

Will ya fly me in too ????  :-)

Kevin
-- 
	/-------------------------------------------------------\
	|	Kevin Minor	...uunet!motcid!minor		|
	\-------------------------------------------------------/

jik@athena.mit.edu (Jonathan I. Kamens) (11/09/90)

In article <45969@sequent.UUCP>, calvin@sequent.sequent.com (Calvin Goodrich) writes:
|> can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
|> stand for in csh? i've seen these used before but couldn't figure them out.
|> for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
|> get an informative answer.

  Then you must have an emasculated version of the csh man page, because mine
documents them as follows:

     After the optional word
     designator can be placed a sequence of modifiers, each pre-
     ceded by a `:'.  The following modifiers are defined:

          h      Remove a trailing pathname component, leaving the head.
          r      Remove a trailing `.xxx' component, leaving the root name.
          e      Remove all but the extension `.xxx' part.
          s/l/r/ Substitute l for r
          t      Remove all leading pathname components, leaving the tail.
          &      Repeat the previous substitution.
          g      Apply the change globally, prefixing the above, e.g. `g&'.
          p      Print the new command but do not execute it.
          q      Quote the substituted words, preventing further substitutions.
          x      Like q, but break into words at blanks, tabs and newlines.

If your csh man page doesn't have this, I suggest you get a new csh man page. 
If it does, then what exactly about it do you not understand?

|> next question: do these things have an equivalent in ksh? apparently ksh
|> doesn't have these little buggers. if they're useful i want to be able to
|> use them in my favorite (imho, anyway) shell.

  I don't use ksh, but from a quick look at the ksh man page, I don't see
anything that can do what all of the modifiers above do.  Some of them can be
done by calling a subprocess such as sed or awk; others can probably be done
using clever quoting, and still others can probably be done with ksh commands
that I'd know about if I used ksh regularly :-).

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

mday@iconsys.icon.com (Matt Day) (11/10/90)

In article <45969@sequent.UUCP> calvin@sequent.sequent.com (Calvin Goodrich) writes:
>can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
>stand for in csh? i've seen these used before but couldn't figure them out.
>for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
>get an informative answer.

Here's a little set of examples (note: I use the tcsh; I don't think the
tcsh and the csh behave differently here, but they might):

> set tmp="/usr/tmp/foo.bar.howdy"
> echo $tmp:e     # (extension) Returns the text to the right of the last '.'
bar
> echo $tmp:h     # (head) Returns the "head" of the path
/usr/tmp
> echo $tmp:t     # (tail) Returns the "tail" (or basename) of the path
foo.bar.howdy
> echo $tmp:r     # (root) Returns everything except for the last '.' extension
/usr/tmp/foo.bar

The :q and :x modifiers are means of setting the high bit on the variable
contents.  :x will set the high bit on all the text in the string except for
spaces and tabs, and :q will set the high bit on everything.  I can't see
very many practical uses for this feature.

As for the :gh, :gt, and :gr modifiers, they seem to do weird things on
my copy of tcsh.  Perhaps they're broken, perhaps I just don't understand
what their purpose is.  In any case, I don't know what they really do.
I think the 'g' means "global" somehow, but I can't tell right off.

Anyway, that ought to get you started.  Those modifiers are really handy,
I use them all the time.  Saves lots of typing, and in shell scripts, they
save a call to "sed".

>next question: do these things have an equivalent in ksh? apparently ksh
>doesn't have these little buggers. if they're useful i want to be able to
>use them in my favorite (imho, anyway) shell.

Sorry, I don't know about the ksh.
-- 
- Matt Day, Sanyo/Icon, mday@iconsys.icon.com || uunet!iconsys!mday

karl_kleinpaste@cis.ohio-state.edu (11/10/90)

calvin@sequent.sequent.com writes:
   can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
   stand for in csh? i've seen these used before but couldn't figure them out.
   for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
   get an informative answer.

Um, RTFM.

A SunOS 4.1 csh(1) man page discusses exactly what these terms mean on
pages 5 and 9.  Same thing for Pyramid OSx 4.4c's csh(1) man page.
From the Pyr version:

(page 5)
     The `:' separating the event specification from the word
     designator can be omitted if the argument selector begins
     with a `|', `$', `*' `-' or `%'.  After the optional word
     designator can be placed a sequence of modifiers, each pre-
     ceded by a `:'.  The following modifiers are defined:

          h      Remove a trailing pathname component, leaving the head.
          r      Remove a trailing `.xxx' component, leaving the root name.
          e      Remove all but the extension `.xxx' part.
          s/l/r/ Substitute l for r
          t      Remove all leading pathname components, leaving the tail.
          &      Repeat the previous substitution.
          g      Apply the change globally, prefixing the above, e.g. `g&'.
          p      Print the new command but do not execute it.
          q      Quote the substituted words, preventing further substitutions.          x      Like q, but break into words at blanks, tabs and newlines.

(page 9)
     The modifiers `:h', `:t', `:r', `:q' and `:x' may be applied
     to the substitutions above as may `:gh', `:gt' and `:gr'.
     If braces `{' '}' appear in the command form then the modif-
     iers must appear within the braces.  The current implementa-
     tion allows only one `:' modifier on each `$' expansion.

--karl

carroll@cs.uiuc.edu (Alan M. Carroll) (11/10/90)

In article <45969@sequent.UUCP>, calvin@sequent.sequent.com (Calvin Goodrich) writes:
>  :e :h :t :r :q :x :gh :gt :gr
>  in csh? 
> 
> next question: do these things have an equivalent in ksh?

No. However, ksh shell has much more powerful mechanisms for doing the equivalent
thing. You can enable a 1 line VI editing mode in ksh using
set -o vi
which then allows you to use standard vi editing commands to modify the
command line (press ESCAPE to enter editing mode). I find this far easier to
use then trying to remember which csh : operator is the right one. Also, you
can regexp search your history (!) and repeat the search (just like vi) if
the first one you find isn't what you wanted. This makes searching for the last
command that referenced file "bob" easy.
-- 
Alan M. Carroll                Barbara/Marilyn in '92 :
Epoch Development Team          + This time, why not choose the better halves?
CS Grad / U of Ill @ Urbana    ...{ucbvax,pur-ee,convex}!cs.uiuc.edu!carroll

crissl@rulcvx.LeidenUniv.nl (Stefan Linnemann) (11/12/90)

In <45969@sequent.UUCP> calvin@sequent.sequent.com (Calvin Goodrich) writes:

>can any of you unix.gods tell me what :e :h :t :r :q :x :gh :gt :gr
>stand for in csh? i've seen these used before but couldn't figure them out.
>for the rtfm'ers in the crowd: yes, i read the man pages on csh but couldn't
>get an informative answer.

Since I'm not (yet) a god, I will not answer.  Would be a bit pointless,
too, after the excellent answers already sent. :-)

>next question: do these things have an equivalent in ksh? apparently ksh
>doesn't have these little buggers. if they're useful i want to be able to
>use them in my favorite (imho, anyway) shell.

Part of their capabilities are implemented in the ksh(1) as follows:
(From the ksh(1) manual page)

     ${parameter#pattern}
     ${parameter##pattern}
	  If the Shell pattern matches the beginning of	the value
	  of parameter,	then the value of this substitution is
	  the value of the parameter with the matched portion
	  deleted; otherwise the value of this parameter is sub-
	  stituted.  In	the first form the smallest matching pat-
	  tern is deleted and in the second form the largest
	  matching pattern is deleted.

     ${parameter%pattern}
     ${parameter%%pattern}
	  If the Shell pattern matches the end of the value of
	  parameter, then the value of this substitution is the
	  value	of the parameter with the matched part deleted;
	  otherwise substitute the value of parameter.	In the
	  first	form the smallest matching pattern is deleted and
	  in the second	form the largest matching pattern is
	  deleted.  In the above, word is not evaluated	unless it
	  is to	be used	as the substituted string, so that, in
	  the following	example, pwd is	executed only if d is not
	  set or is null:
	  echo ${d:-$(pwd)}

>first person to give me a good answer to both questions gets a free cup of
>coffee at my company's cafeteria.  :]

With the airline ticket attached, this would be an even more expensive
cup of coffee, than for the guy from Illenois. :-)

>thanx guys,

>calvin.

You're welcome.
Stefan.

+---------                                                  ---------+
| Stefan M. Linnemann, a.k.a. crissl@rulcvx.LeidenUniv.nl            |
|                                                                    |
  Life is like the odd bit of string: it should be long enough to do  
  Something Extremely Useful with it, but for all of the really neat  
| things we think of, it's just too short.                           |
| -- Me, 1990, as far as I know; correct me if I'm wrong.            |
+---------                                                  ---------+

dbert@pogo.gnu.ai.mit.edu (Douglas Siebert) (03/21/91)

OK, here's another good one:  in csh you can type ctrl-Z to stop a process
and then use fg to return to it, right?  Now, is there any way to (before
you return to the process using fg) pipe some output to that process in
some way so that it will be like the process itself received that as input?
Either through redirecting that process' standard input from the outside
or piping some output some way directly to that process?

Thanks for any help (and if there *is* some way I can do something similar in
a shell other than csh, I'd be interest in that as well, especially in sh or
ksh since those are relatively universal)
--
________________________________________________________________________
Doug Siebert                                     dbert@gnu.ai.mit.edu
MBA Student (2nd year)
The University of Iowa

jik@athena.mit.edu (Jonathan I. Kamens) (03/21/91)

In article <1991Mar21.005808.12432@mintaka.lcs.mit.edu>, dbert@pogo.gnu.ai.mit.edu (Douglas Siebert) writes:
|> Now, is there any way to pipe some output to that process in
|> some way so that it will be like the process itself received that as input?
|> Either through redirecting that process' standard input from the outside
|> or piping some output some way directly to that process?

  If you want to muck around in the kernel address space and do gross, hideous
things to convince the process to get its input from somewhere else
temporarily, or to push input into the input queue of the process, then sure.

  If you started the process with some sort of session manager like pty,
possibly.

  Otherwise, almost certainly not.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

hansm@cs.kun.nl (Hans Mulder) (03/27/91)

In article <1991Mar21.005808.12432@mintaka.lcs.mit.edu> dbert@pogo.gnu.ai.mit.edu (Douglas Siebert) writes:
>OK, here's another good one:  in csh you can type ctrl-Z to stop a process
>and then use fg to return to it, right?  Now, is there any way to (before
>you return to the process using fg) pipe some output to that process in
>some way so that it will be like the process itself received that as input?
>Either through redirecting that process' standard input from the outside
>or piping some output some way directly to that process?

Well, if you want to feed less than 255 bytes into your process, you might
try using ioctl(0,TIOCSTI,ch) if your system supports it.  This plugs bytes
into the terminal driver's input queue and from where they will be read by
the next process that tries to read anything from your terminal.

So you could start by ioctling the characters 'f', 'g' and '\n', followed
by some input for your stopped process.  When your ioctling program is done,
csh reads the fg command and resumes your stopped process.

Or you could type a command like "my_ioctling_prog; fg".

>Thanks for any help (and if there *is* some way I can do something similar in
>a shell other than csh, I'd be interest in that as well, especially in sh or
>ksh since those are relatively universal)

The sh on most Unix systems is the one written by Steve Bourne.  That
one does not handle ctrl-Z in any way.  If the sh in your system does
handle ctrl-Z, then it is the rewrite by David Korn.  That one handles
ctrl-Z in pretty much the same way as csh.  Ksh is the name of Korn's
shell on systems where sh is Bourne's.


But maybe you really wanted something along these lines:

#!/usr/local/bin/perl

open(OUT,"|xxxxx");
select(OUT);
$|=1;

while(<>) {
    if (/\|$/) {
        open(PIPE,$cmd=$_);
        while(<PIPE>) {
            print OUT;
        }
        close(PIPE);
	print STDERR "Problem $? while running $cmd" if $?;
    } else {
	print OUT;
    }
}

This will copy your input lines into "xxxxx", except those that end
in a '|'.  Such lines are interpreted as commands, and their output
is piped into "xxxxx".


Have a nice day,

Hans Mulder	hansm@cs.kun.nl

morgan@chaos.cs.brandeis.edu (Dylan Kaufman) (05/02/91)

Hi,

I am trying to write a script which will be run by cron and which will
notify me if someone somewhere is logged on.  What I mean by that is
that the script will check to see whether I am logged on (if not, it
doesn't need to bother...) and then (assuming I am) check to see
whether there is someone logged onto another account on another
computer.  If there is, it should then determine what tty I am logged
onto and either cat or write to my tty... the problem is that it
doesn't work...  I get mail from root saying that my cron output is 

stty: : Not a typewriter.

The same thing happens when I try having it send me mail rather than
using cat or write.

The code I have is as follows:

#!/bin/csh
set a=`f|fgrep -c morgan`
if ($a != 0) then
	set b=`finger @eris.berkeley.edu|fgrep -c morgan`
	if ($b != 0) then
		set c=`w morgan|fgrep morgan|cut -f4 -d' '`
		cat ~/cronfile > /dev/$c
	endif
endif

Any help would be appreciated.  

Thank you in advance,
--
-<>Dylan<>-                     MA EMT-M, CA EMT-1A, BEMCo 107
Dylan Kaufman 			Major in Computer Science
morgan@chaos.cs.brandeis.edu	Brandeis University, Waltham, MA
------<< Support your local Emergency Medical Services >>-------
"Don't ask me, I'm just improvising" -RUSH

kre@cs.mu.oz.au (Robert Elz) (05/04/91)

morgan@chaos.cs.brandeis.edu (Dylan Kaufman) writes:

>stty: : Not a typewriter.

I'd guess that your .cshrc has an "stty" command in it, which will
break all kinds of things (it belongs in .login).  You could fix
that, or try

	#!/bin/csh -f

but you'd be much better off to use

	#!/bin/sh

and rewrite the script in sh.   Fix your .cshrc anyway.

kre