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
  • 7,557
  • 1
  • 30
  • 47
John
  • 1,825
  • 12
  • 32
  • 43

4 Answers4

11

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
  • 324,833
  • 88
  • 366
  • 429
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
  • 129,246
  • 33
  • 194
  • 186
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
  • 38,637
  • 12
  • 58
  • 67
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.
  • 102,141
  • 38
  • 210
  • 325
  • 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