1827808 Members
2142 Online
109969 Solutions
New Discussion

Troubles with cshell

 
SOLVED
Go to solution
Alex Lavrov.
Honored Contributor

Troubles with cshell

Hello all,

well, for the first time in my life I'm forced to work with cshell and have some problems.

This is my code:

if ("$field_1" =~ ^[a-z]) then
echo "$field_1 IS OK"
else
echo "$field_1 NOT OK!!!"
endif

As I understand it, it matches the strings that begin with a small letters. But ...
As output for "2F2" and "field_1" I get:
2F2 IS OK

Any ideas what might be the problem?

Thanx :)
I don't give a damn for a man that can only spell a word one way. (M. Twain)
22 REPLIES 22
TwoProc
Honored Contributor

Re: Troubles with cshell

Can I sell you a bushel of bash instead?
We are the people our parents warned us about --Jimmy Buffett
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Oh, believe me, I wan't and can do it with bash ....

But it must be done with cshell and the worst part is it must run on Sun OS 5.9 ...


:-)
I don't give a damn for a man that can only spell a word one way. (M. Twain)
John Kittel
Trusted Contributor

Re: Troubles with cshell

I don't know csh either. I'm sure if you wait, you'll get an answer from someone who does.

But in the mean time I played with it a little. I wonder if the "!" characters in the echo statement are goofing it up? Try removing them. Just a hunch.
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

I doubt.

The script doesn't even get there. For some reason "if" returns true.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Robert-Jan Goossens_1
Honored Contributor

Re: Troubles with cshell

I'll check some slowaris c-hell scipts first thing tomorrow morning. Let you know if I can find a clue :-)

Good to see you back in the ITRC Alex! How is the study?
TwoProc
Honored Contributor

Re: Troubles with cshell

#!/usr/bin/csh -c /usr/local/bin/bash
We are the people our parents warned us about --Jimmy Buffett
John Kittel
Trusted Contributor

Re: Troubles with cshell

Uh, sorry. Hope you don't mind another suggestion...

Your example works for me when I change expression in if statement, like this:

if ("$field_1" =~ [a-z]* ) then

Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Robert:

Well, studies are pretty hard ... As you can see they are torchering me with csh on Sun OS ... And after that I have to write silly game in assembler for PDP11 (think it's grandpa of VAX). Beside that it's OK, but can't say I'm having fun, tho :)


John:

I got "0: Event not found."

I don't give a damn for a man that can only spell a word one way. (M. Twain)
John Kittel
Trusted Contributor

Re: Troubles with cshell

Yes, that is caused by the !s. Remove them and then ( in conjunction with the modified expression ) it may work.

From the HP-UX 11.11 man page:

In an international environment, character ordering is determined by the setting of LC_COLLATE, rather than by the binary ordering of character values in the machine collating sequence. This brings with it certain attendant dangers, particularly when using range expressions in file name generation patterns. For example, the command,

rm [a-z]*

might be expected to match all file names beginning with a lowercase alphabetic character. However, if dictionary ordering is specified by LC_COLLATE, it would also match file names beginning with an uppercase character (as well as those beginning with accented letters). Conversely, it would fail to match letters collated after z in languages such as Norwegian.

The correct (and safe) way to match specific character classes in an international environment is to use a pattern of the form:

rm [[:lower:]]*
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Well, I removed !s, still no good ...
LC_COLLATE is not even set. The system is pure english.

set field_1 = "2F2"

if ("$field_1" =~ ^[a-z]) then
echo "$field_1 IS OK"
endif


Output:
2F2 IS OK
I don't give a damn for a man that can only spell a word one way. (M. Twain)
John Kittel
Trusted Contributor

Re: Troubles with cshell

remove !s AND ALSO fix expression...
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Nope, tried all variations.

This line works fine:

if (`echo "$field_1" | grep '^[A-Za-z]' | wc -l`)

but with "=~" it doesn't ... I think I miss something in if.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
John Kittel
Trusted Contributor

Re: Troubles with cshell

ok, sorry, I promise this will be my last post on this. I tried it the way you first presented it and had the same problem as you indicated. Now, with modifications I indicated, it works for me, with HP-UX csh, as following example shows.

# cat tc1.sh
#!/usr/bin/csh
setenv field_1 $1
echo "1 = $1, field_1 = $field_1"
if ("$field_1" =~ [a-z]* ) then
echo "$field_1 IS OK"
else
echo "$field_1 NOT OK"
endif
exit

# ./tc1.sh 2F2
1 = 2F2, field_1 = 2F2
2F2 NOT OK

# ./tc1.sh hello
1 = hello, field_1 = hello
hello IS OK

# ./tc1.sh Hello
1 = Hello, field_1 = Hello
Hello NOT OK
Steven E. Protter
Exalted Contributor

Re: Troubles with cshell

I didnot go to sleep.

#!/usr/bin/csh
setenv field_1 $1
echo "1 = $1, field_1 = $field_1"
if ("$field_1" =~ [a-z]* ) then
echo "$field_1 IS OK"
else
echo "$field_1 NOT OK"
endif
exit

This script tests out okay on Linux c-shell.

I think the suggestion from the previous post will also work on sun.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Thanx, I'll try to make it work.

The final regex, should match words that star with a-z or A-Z and after that contains only numbers and letters.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Mike Stroyan
Honored Contributor

Re: Troubles with cshell

Apart from the
echo "!!!"
being affect by history substitution and the leading "^" not matching front of string like bash, csh has a problem with locale. If you use a locale like LANG=en_US.iso88591 then the [a-z] range will include aBbCcDd...Zz because the collation order doesn't group the lowercase characters together. I don't know of any locale that would make [a-z] match 2.
The csh manual suggests using [[:lower:]] for a locale independent way to match lower case in file name patterns. That does work for file names. It doesn't work for =~.
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Oh I see ... so "^" doesn't work as it should normally work, that solves one mystery ...

And now I just need something after [A-Za-z] at the begining of the word, that will match any letter or number, like:
Alex342
aL3Lssd342

But not:
aL34_234
a324 234
etc.

I just can't make some stuff I found working in cshell ..

Thanx in advance.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Mike Stroyan
Honored Contributor

Re: Troubles with cshell

If you happened to have a typo that spelled "than" instead of "then", csh would presume that "than" was some executable name. The if would determine whether to execute "than". It would unconditionally execute the echo line. It would silently pass by the unexpected endif line.

if ( "$field_1" =~ [a-z]* ) than
echo "$field_1 IS OK"
endif
Peter Nikitka
Honored Contributor

Re: Troubles with cshell

Hi,

perhaps it is better to use a switch/case construct, which uses filename matching patterns:

switch ($field_1)
case [a-z]*:
echo starting lower case
breaksw
case [A-Z]*:
echo starting upper case
breaksw
default:
echo starting not with a letter
endsw


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Thanx, the issue with starting letter is solved :)

Now I'm looking for regexp that beside cheking that word starts with a letter, will check that the other symbols are only letters or numbers.

All the stuff I try, just don't work in csh ... (tcsh).


thanx in advance.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Peter Nikitka
Honored Contributor
Solution

Re: Troubles with cshell

Hi,

to come to your request

Now I'm looking for regexp that beside cheking that word starts with a letter, will check that the other symbols are only letters or numbers.

Starting with my switch/case but exiting/breaking in
default:

you can continue in stripping all legal chars and looking at the rest (needs tcsh):

set ok=`echo $field_1 | tr -d '[:lower:][:upper:][0-9]'`
if ($%ok) then
echo $field_1 contains $%ok illegal chars
exit 1
endif

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Alex Lavrov.
Honored Contributor

Re: Troubles with cshell

Thanx all for the help! :)

Have a nice weekend.
I don't give a damn for a man that can only spell a word one way. (M. Twain)