8

In one of my shell script, I'm seeing

if [[ ! -d directory1 || ! -L directory ]] ; then

What does -d and -L option mean here? Where can I find information about the options to use in an if condition?

doubleDown
  • 8,048
  • 1
  • 32
  • 48
John
  • 2,035
  • 13
  • 35
  • 44

4 Answers4

12

You can do help test which will show most of the options accepted by the [[ command.

You can also do help [ which will show additional information. You can do help [[ to get information on that type of conditional.

Also see man bash in the "CONDITIONAL EXPRESSIONS" section.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

The -d checks whether the given directory exists. The -L test for a symbolic link.

The File test operators from the Advanced Bash-Scripting Guide explain the various options. And here is the man page for bash which can also be found by typing man bash in the terminal.

Josh
  • 5
  • 3
Levon
  • 138,105
  • 33
  • 200
  • 191
1

bash has built-in help with the help command. You can easily find out the options to a bash built-in using help:

$ help [[
...
Expressions are composed of the same primaries used by the `test' builtin
...
$ help test
test: test [expr]
    Evaluate conditional expression.
    ...
    [the answer you want]
camh
  • 40,988
  • 13
  • 62
  • 70
0

In Bourne shell, [ and test were linked to the same executable. Thus, you can find a lot of the various tests available in the test manpage.

This:

if [[ ! -d directory1 || ! -L directory ]] ; then

is saying if directory1 is not a directory or if directory is not a link.

I believe the correct syntax should be:

if [[ ! -d $directory1 ] ||  [ ! -L $directory ]] ; then

or

if [[ ! -d $directory1 -o ! -L $directory ]] ; then

Is the line in your OP correct?

David W.
  • 105,218
  • 39
  • 216
  • 337
  • The line in the OP is correct; you may be confusing bash syntax with POSIX syntax. Bash's `[[` supports `||` and whatnot; POSIX `[` does not. To do the same thing in POSIX shell, you'd do `if [ -d "${directory1}" ] || ! [ -h directory ]; then`. The `-o` syntax is specified by POSIX, but it is obsolescent (it causes parsing ambiguities). – Richard Hansen Jul 04 '13 at 02:30