Operating System - HP-UX
1847084 Members
5256 Online
110262 Solutions
New Discussion

Re: Conditional Expressions doubt

 
SOLVED
Go to solution
Ester Saez
Advisor

Conditional Expressions doubt

I would like to know what is the difference (if any) between using "-h" and "-L" to know if a file is a symbolic link. And also the difference between using "-a" and "-e" to know if a file exists. On line help doen't say if there is or if there is not a difference, so I like to be sure.
6 REPLIES 6
Jeroen Peereboom
Honored Contributor

Re: Conditional Expressions doubt

Ester,

what's the context?

If I look at 'test' manpage, I cannot find -L, -e and -a.

So, what's the language are you 'writing' in?

JP
Jeff_Traigle
Honored Contributor

Re: Conditional Expressions doubt

I know it's Monday and I might be missing something, but what are you using to test this that you see these flags in the man page? test(1) only shows -h for symbolic links and -a for binary AND operator... no -L or -e at all... file existence is -f.
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Conditional Expressions doubt

You really have to identify the shell that you are using for a complete difference but if I assume that you are using the POSIX shell then there is no difference between -L and -h as both will test if a file is a symbolic link -- but only if using the shell's internal test operator. -L means nothing to the test command. Also, -e is only known to the shell's internal test operator and -a is actually an AND operator to the test command.

The best way to avoid all this confussion is to adopt the "double brackets" approach:

This version will always use the shell's internal test operator (and is thus more efficient):
if [[ $(a) -gt $(b) && -f "${MYFILE}" ]]
then
fi

This equivalent version will call the external test command:

if [ $(a) -gt $(b) -a -f "${MYFILE}" ]
then
fi

Note that the syntax is a little different in the two cases.

If you must run under the Bourne shell for portability then you should always use the test command and stay within its more limited set of operators but if you know you will always be ruunning on a Korn or POSIX shell or bash then I would favor the internal test operator.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Conditional Expressions doubt

Oops, the $a's and $b's should have had {}'s around them instead of ()'s:

if [[ ${a} -gt ${b} && -f "${MYFILE}" ]]
then
fi
If it ain't broke, I can fix that.
Ester Saez
Advisor

Re: Conditional Expressions doubt

Sorry for being so cryptic. I am talking abour Shell Script, using sh shell. "man sh-posix" sayig that -h and -L can be used to know if a file is a symbolic link nad that -a and -e can be used to know if a file exists.
Check this example:

"pru.sh" 17 lines, 183 characters
file="/tmp/pru/sbinsh"
if [ -a ${file} ]; then
echo "-a"
fi
if [ -e ${file} ]; then
echo "-e"
fi
if [ -h ${file} ]; then
echo "-h"
fi
if [ -L ${file} ]; then
echo "-L"
fi

# ll
total 2
-rwx------ 1 root sistemas 183 Apr 5 16:46 pru.sh
lrwxrwxrwx 1 root sistemas 8 Apr 5 16:43 sbinsh -> /sbin/sh
# ./pru.sh
-a
-e
-h
-L

Sanjay Kumar Suri
Honored Contributor

Re: Conditional Expressions doubt

There is no difference as your output also proves.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.