Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "test -a file" and "test file -ef file"

Tags:

ksh

qnx

QNX (Neutrino 6.5.0) uses an open source implementation of ksh as its shell. A lot of the provided scripts, including the system startup scripts, use constructs such as

if ! test /dev/slog -ef /dev/slog; then
    # do something
fi

to check whether a resource manager exists or not in the filesystem. I've searched and could only find very dray explanations that -ef checks to see whether the two parameters are in fact the same file. Since the filename specified is the same it seems to just reduce to checking that the file exists.

I have checked the behaviour of test -a and test -e (both seem to check for file existance of any type of file according to the various docs I've read) and they seem to also work.

Is there any difference in the checks performed between -ef and -a/-e? Is using -ef some kind of attempt to protect against a race condition in the existence of the file?

like image 389
tinman Avatar asked Jul 05 '12 13:07

tinman


People also ask

What is the difference between a test and an experiment?

Tests can never be experiments, but experiments can be tests. Like one can go for ‘blood tests’ and not ‘blood experiments’. While ‘experimenting’ can lead to the invention of new things, ‘tests’ don’t lead to the invention of new things; rather, they are done before implementation on a larger scale.

What is the difference between a text file and text text file?

Text files also store data in sequential bytes but bits in text file represents characters. Text files are less prone to get corrupted as any undesired change may just show up once the file is opened and then can easily be removed.

What is the difference between test case and test script?

Test Case is a step by step procedure to test any functionality of the software application/product. Test Script is set of instructions or a short program to test any functionality of software application/product. 2. Test Case is a manual approach of software testing.


2 Answers

Reviewing the strace on Ubuntu Linux's copy of ksh reveals no substantial differences. One call to stat vs two.

$ strace test /tmp/tmp.geLaoPkXXC -ef /tmp/tmp.geLaoPkXXC

showed this:

mmap(NULL, 7220736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f11dc80b000
close(3)                                = 0
stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
close(1)                                = 0
close(2)                                = 0

...whereas

$  strace test -a /tmp/tmp.geLaoPkXXC

showed this:

fstat(3, {st_mode=S_IFREG|0644, st_size=7220736, ...}) = 0
mmap(NULL, 7220736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6b49e2b000
close(3)                                = 0
stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
close(1)                                = 0
close(2)                                = 0

One stat vs two.

$ ksh --version
  version         sh (AT&T Research) 93u 2011-02-08
like image 78
Brian Cain Avatar answered Oct 14 '22 06:10

Brian Cain


We don't know how the code use the stat exactly without code, we need to find the difference via the code.

/* code for -ef */
return (stat (argv[op - 1], &stat_buf) == 0
                  && stat (argv[op + 1], &stat_spare) == 0
                  && stat_buf.st_dev == stat_spare.st_dev
                  && stat_buf.st_ino == stat_spare.st_ino);


/* code for -e/-a */
    case 'a':                   /* file exists in the file system? */
    case 'e':
      return stat (argv[pos - 1], &stat_buf) == 0;

So, if the names are the same and two stat() with the same name will return the same value, then, test -a/-e file is the same as test file -ef file. We know the first condition is true, and we know the second condition is also true from the comments from @tinman

like image 43
Lai Jiangshan Avatar answered Oct 14 '22 05:10

Lai Jiangshan