> According to several man pages I got, the commands are -h, -l and -L. > What should I choose when doing the shell script programming?
Welcome to the wonderful world of Unix, where people never seem te be able to agree on such simple things. The answer is, unfortunately, "It depends what Unix you are on".
> Correct me if I'm wrong: > I think the gnu flavor is -L, that is bash's buildin test and test from > gnu (.../gnu/test) use -L. (and ksh)
Correct.
> sh also has buildin test? because:
Yes.
> and sh buildin test for symbolic link files is -h and (?) -L. > Is above correct?
See the local man sh, where the builtin 'test' is documented.
> But why when it comes to real testing, I got weird results?
> sh > $ ls -l index.htm > lrwxrwxrwx 1 tongsun glan 31 Nov 5 11:11 index.htm -> > ../../... > $ [ -h index.htm ] && echo aa > aa > $ [ ! -h index.htm ] && echo aa > $
> hmm..., seems good, but how about:
> sh > $ [ -L index.htm ] && echo aa > test: argument expected > why?
The -L is not a recognized flag for the built-in '[' command.
> besides,
> $ test " -l index.htm" && echo no > no > $ test "! -l index.htm" && echo no > no > $ test "-h index.htm" && echo aa > aa > $ test "! -h index.htm" && echo aa > aa ... > what's going here, why test is always true?
Because you pass all these tests A SINGLE argument. If you write "-h index.htm" (in double quotes) then that is a single argument which is evaluated a test "is this string not empty". Since this is true in ALL the above cases, you get the above results.
try:
$ test -l index.htm && echo no
And it will work much better.
Ruurd.
``test -h'' works on FreeBSD 4.0, HP-UX 11.10, and DEC UNIX 5.0.
ksh on DEC UNIX 5.0:
$ type test test is a shell builtin $ touch File1 $ ln -s File1 File2 $ test -h File1 && echo "File1 is a symlink." $ test -h File2 && echo "File2 is a symlink." File2 is a symlink. $ /bin/test -h File1 && echo "File1 is a symlink." $ /bin/test -h File2 && echo "File2 is a symlink." File2 is a symlink. $
The behavior is the same for HP-UX and FreeBSD.
> and sh buildin test for symbolic link files is -h and (?) -L. > Is above correct?
Yes.
> $ test " -l index.htm" && echo no > no
And I've got no evidence an "-l" flag exists. Plus arguments aren't going to be separated if you put quotes around them! All the following tests will pass!
Example:
$ test "abc" && echo Passed. Passed. $ test "abcdeffg hijklmnop" && echo Passed. Passed.
Chris Costello
documented on: 1999.11.09 Tue 10:45:41