[comp.unix.shell] Csh: first character of an arg is '-'

jak@cs.brown.edu (Jak Kirman) (10/07/90)

Context: SunOS 4.1, /bin/csh

I want to be able to test whether the first character of an argument to
a csh-script is '-'.  The man page makes it sound like ~= should work
for this, but I could make no sense of that portion of the man page, and
was not able to find any case where the result of ~= was non-0 except
the trivial case where the right-hand side contained no wildcards and
was the same as the left...

I do *not* want to have to exec a program; I want this to be fast.

Please do not tell me to use sh -- I know it is better for scripts, but
the syntax is rather arcane, and I don't have the time to learn it right
now, although eventually I will probably have to.  If you are quite
certain this is not possible in csh, but it is in sh, I would appreciate
a quick example of sh usage.

As an aside, is there any way to find the first character of a word in
csh or sh?

Thanks.
                                Jak                            jak@cs.brown.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
He had been kicked in the head by a mule when young, and believed
everything he read in the Sunday papers.
                                                              -- George Ade

meissner@osf.org (Michael Meissner) (10/07/90)

In article <JAK.90Oct6202505@bimini.cs.brown.edu> jak@cs.brown.edu
(Jak Kirman) writes:

| Context: SunOS 4.1, /bin/csh
| 
| I want to be able to test whether the first character of an argument to
| a csh-script is '-'.  The man page makes it sound like ~= should work
| for this, but I could make no sense of that portion of the man page, and
| was not able to find any case where the result of ~= was non-0 except
| the trivial case where the right-hand side contained no wildcards and
| was the same as the left...
| 
| I do *not* want to have to exec a program; I want this to be fast.

The standard trick that is used in the /bin/sh case is to use a case
(read switch for csh) statement.  For example:

	switch ($1)
	case -*:
		echo "$1 is an option."
		breaksw

	default:
		echo "$1 is not an option."
		breaksw
	endsw

| Please do not tell me to use sh -- I know it is better for scripts, but
| the syntax is rather arcane, and I don't have the time to learn it right
| now, although eventually I will probably have to.  If you are quite
| certain this is not possible in csh, but it is in sh, I would appreciate
| a quick example of sh usage.

/bin/sh is more arcane than csh?  Surely thou jestest......  Any
interpreter that requires spaces to be exactly right is a hack, and
not an integrated language.  Ok, ok, enough with the csh taunting.
Here is the way to do it with the one true shell (whoops :-):

	case "$1" in
		-*)	echo "$1 is an option.";;
		*)	echo "$1 is not an option.";;
	esac

| As an aside, is there any way to find the first character of a word in
| csh or sh?

If you don't mind enumerating the possibilities, the case/switch
example is one such approach.  In musty, ancient versions of /bin/sh
that don't have test built in (ie, Ultrix, true BSD 4.[23], etc.),
case is a faster way of testing for equality than test (aka, '[').

--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Do apple growers tell their kids money doesn't grow on bushes?