Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Csh - How to distinguish between symbolic link and directory in script?

1,356 views
Skip to first unread message

Jan Hovius

unread,
Jun 29, 2004, 5:25:20 AM6/29/04
to
Hello,

I have a, what i think very trivial, problem:

I have a cshell script in which I want to test whether an entry is
either a local file, local directory or a symbolic link.

I know that in bourne shell it is pretty easy to test for a symbolic
link using "test -h" or "test -L" but this doesn't distinguish between
a directory or link in csh!
I tried:

if ( -h <item> ) then
....

but this gives me the following error message: "if: badly formed
number" (apparently the builtin test functionality of the csh doesn't
support this)

So I tried the following construct

if ( ! `test -h <item>` ) then
.....

but as said this can't make a difference between a directory and a
link.

Of course one could extract information about the <item> using the
'ls' command (and do some post processing on the outcome) but I have
the feeling there should be a more direct way to find out.

Any idea is welcome.

Rgds,

Jan Hovius.

Bruce Barnett

unread,
Jun 29, 2004, 7:38:12 AM6/29/04
to
Jan.H...@nsc.com (Jan Hovius) writes:

> I have a cshell script in which I want to test whether an entry is
> either a local file, local directory or a symbolic link.
>
> I know that in bourne shell it is pretty easy to test for a symbolic
> link using "test -h" or "test -L" but this doesn't distinguish between
> a directory or link in csh!
> I tried:
>

I don't know what -h means for tcsh.

man tcsh(1) gives

f Plain file
d Directory
l Symbolic link (+) *
L Applies subsequent operators in a multiple-opera-
tor test to a symbolic link rather than to the
file to which the link points (+) *


So try:

if ( -l "$1" ) then
echo $1 is a symbolic link
endif

if ( -d "$1" ) then
echo $1 is a directory
endif

Now if you use "-f" it will match either a symbolic link to a plain
file, or a plain file. So test for -l first if you want to distinquish this.


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Bill Marcum

unread,
Jun 29, 2004, 4:38:59 PM6/29/04
to
On 29 Jun 2004 02:25:20 -0700, Jan Hovius
<Jan.H...@nsc.com> wrote:
> Hello,
>
> I have a, what i think very trivial, problem:
>
> I have a cshell script in which I want to test whether an entry is
> either a local file, local directory or a symbolic link.
>
> I know that in bourne shell it is pretty easy to test for a symbolic
> link using "test -h" or "test -L" but this doesn't distinguish between
> a directory or link in csh!
> I tried:
>
> if ( -h <item> ) then
> ....
>
> but this gives me the following error message: "if: badly formed
> number" (apparently the builtin test functionality of the csh doesn't
> support this)
>
> So I tried the following construct
>
> if ( ! `test -h <item>` ) then
> .....
>

Back quotes give you the output of a command, but test has no output,
only an exit status. In csh you can test the exit status of a
command with if { command } .

--
The truth you speak has no past and no future. It is, and that's all it
needs to be.

Bill Marcum

unread,
Jun 29, 2004, 6:42:56 PM6/29/04
to
On 29 Jun 2004 11:38:12 GMT, Bruce Barnett
<spamhater99+...@grymoire.com> wrote:
> I don't know what -h means for tcsh.
>
> man tcsh(1) gives
>
> f Plain file
> d Directory
> l Symbolic link (+) *
> L Applies subsequent operators in a multiple-opera-
> tor test to a symbolic link rather than to the
> file to which the link points (+) *
>
It also says:
Throughout this manual, features of tcsh not found in most csh(1)
implementations (specifically, the 4.4BSD csh) are labeled with (+),

Villy Kruse

unread,
Jun 30, 2004, 3:07:22 AM6/30/04
to
On 29 Jun 2004 02:25:20 -0700,
Jan Hovius <Jan.H...@nsc.com> wrote:


> Hello,
>
> I have a, what i think very trivial, problem:
>
> I have a cshell script in which I want to test whether an entry is
> either a local file, local directory or a symbolic link.
>
> I know that in bourne shell it is pretty easy to test for a symbolic
> link using "test -h" or "test -L" but this doesn't distinguish between
> a directory or link in csh!


Try

test -h filename -a -d filename

That is, both the directory test and the symbolic test is true.


Villy

Villy Kruse

unread,
Jun 30, 2004, 3:14:01 AM6/30/04
to
On 29 Jun 2004 11:38:12 GMT,
Bruce Barnett <spamhater99+...@grymoire.com> wrote:


> Jan.H...@nsc.com (Jan Hovius) writes:
>
>> I have a cshell script in which I want to test whether an entry is
>> either a local file, local directory or a symbolic link.
>>
>> I know that in bourne shell it is pretty easy to test for a symbolic
>> link using "test -h" or "test -L" but this doesn't distinguish between
>> a directory or link in csh!
>> I tried:
>>
>
> I don't know what -h means for tcsh.
>

It has meaning for the test command which lives in /bin or /usr/bin.

>
> if ( -l "$1" ) then
> echo $1 is a symbolic link
> endif
>
> if ( -d "$1" ) then
> echo $1 is a directory
> endif
>

Which, of course, is the builtin feature for testing file types without
using the test command.


Villy

Bruce Barnett

unread,
Jun 30, 2004, 8:01:12 AM6/30/04
to
Villy Kruse <v...@station02.ohout.pharmapartners.nl> writes:

>> I don't know what -h means for tcsh.
>>
>
> It has meaning for the test command which lives in /bin or /usr/bin.

That was my point. Thanks.

Jan Hovius

unread,
Jul 14, 2004, 4:30:38 AM7/14/04
to
Thank you all for responding to my problem. I, in the meantime however
decided to migrate the scripts to bourne shell in which I have no
problem whatsoever with it. Nevertheless I will use the ideas
reflected in this thread in other scripts.

Again, thank you!

Villy Kruse <v...@station02.ohout.pharmapartners.nl> wrote in message news:<slrnce4pl...@station02.ohout.pharmapartners.nl>...

0 new messages