Shell Script -x Flag Meaning

15,393

Solution 1

if just checks for result of command following it. [ is not (at least not always) an operator, it's small utility called 'test'.

From its documentation:

-x file
                             True if file exists and is  exe-
                             cutable.   True  indicates  only
                             that the execute flag is on.  If
                             file  is a directory, true indi-
                             cates that file can be searched.

(and yes, ! is obviously negation)

For similar evualation flags, documentation is available here: http://illumos.org/man/1/test

Solution 2

The ! -x conditional means the file or directory doesn't have the executable bit set for the current user. The help is a little less clear about the fact that it applies to directories too, but it says:

$ help test | fgrep -- '-x'
      -x FILE        True if the file is executable by you.
Share:
15,393
Baxter
Author by

Baxter

Updated on June 04, 2022

Comments

  • Baxter
    Baxter almost 2 years

    I have a #!/bin/sh script that has the following line:

    if [ ! -x "$TEST_SLAPD" ]
    

    $TEST_SLAPD is the full path to a .bat file.

    I am wondering what the -x flag means in the context of that if statement?

  • Baxter
    Baxter almost 12 years
    If that is returning false and the file is definitely there is it likely a permissions issue with the shell script trying to execute the .bat file?
  • Todd A. Jacobs
    Todd A. Jacobs almost 12 years
    @Baxter Yes. That's why -r and -x are often a more useful than just testing for a file's existence with -f.