Technical article 105461

test -e <filename> using Bourne shell gives the error "test: argument expected".

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

I am trying to test for the existence of a file using the "-e" option to the test(C) command (or its counterpart, "["). For example, to test for the file "filename", use:

test -e filename
<or>
[ -e filename ]
The "test" function is built-in to both the Bourne shell, sh, and
the Korn shell, ksh.  Both sh(C) and ksh(C) man pages refer to the
test(C) man page for detailed description.  The "-e" option is one
of the options detailed in this man page.
The "-e" option seems to work fine under ksh.  But under sh, I get
the following error:
test: argument expected

Cause

The "-e" option to test was introduced in SCO OpenServer Release 5. It was incorporated into the ksh program, but this feature was not added to the sh program.

Solution

This has been reported to SCO Engineering. If you are using the Bourne shell, sh, and wish to test for a file's existence, there are several workarounds you can use:

1. Run in the Korn shell, ksh, environment.  At the Bourne
   shell prompt, type:
ksh -o vi
-or-
exec ksh -o vi

2. Run shell scripts that include "test -e" in the ksh environment by adding, as the first line of such scripts, the following:

#!/bin/ksh

3. Modify "-e" test statements and the /bin/test and /bin/[ files. To modify the "-e" test statements, change all of these statements in your scripts as:

change:  "test -e"   -->  "/bin/test"
-and-
change:  "["         -->  "/bin/["

Next, modify the /bin/test and /bin/[ files by adding the following as the first line to each file:

#!/bin/ksh
4. Alternative to the "-e" option, reconstruct your test using
   one or more of the other test options available, as "-f",
   "-d", "-r", "-s", etc.

See also

Manual pages: test(C), sh(C), ksh(C)

From the Xinuos technical article database, record 105461. Reproduced with permission. The original is at www.sco.com/ta/105461, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.