WhoAmI
WhoAmI

Reputation: 1133

BASH OR statement

I want to execute echo only if one of the folders are not found? However AEM_SEGMENTSTORE_LOCATION_AZURE is found but I still get "echo not found"

 #!/bin/bash
    AEM_SEGMENTSTORE_LOCATION="/opt/day/${AEM_RUNMODE}/crx-quickstart/repository/segmentstore"
    AEM_SEGMENTSTORE_LOCATION_AZURE="/opt/day/crx-quickstart/repository/segmentstore"
     [[ ! -d ${AEM_SEGMENTSTORE_LOCATION} ]] || [[  ! -d ${AEM_SEGMENTSTORE_LOCATION_AZURE} ]] && echo "not found"

Upvotes: 0

Views: 49

Answers (2)

tripleee
tripleee

Reputation: 189689

The precedence is wrong. You seem to be looking for AND anyway. You can easily fix this by changing it to

if [[ ! -d "$AEM_SEGMENTSTORE_LOCATION" ]] &&
[[ ! -d "$AEM_SEGMENTSTORE_LOCATION_AZURE" ]]
then
    echo "$0: not found" >&2
fi

Notice also proper quoting of your variables {see When to wrap quotes around a shell variable; braces do not quote, and were basically useless here) and probably avoid uppercase variable names if these are private variables of yours (uppercase is reserved for system variables). Finally, the diagnostic message should probably go to standard error, and include the script's name; it should probably also say what was not found.

Upvotes: 0

chepner
chepner

Reputation: 531918

In general, don't mix || and &&. The precedence is not what you expect. a || b && c is equivalent to (a || b) && c, but a && b || c is not the same as (a && b) || c. Get in the habit of using a proper if statement.

if [[ ! -d "$AEM_SEGMENTSTORE_LOCATION" || ! -d "$AEM_SEGMENTSTORE_LOCATION_AZURE" ]]; then
    echo "not found"
fi

or

if ! [[ -d "$AEM_SEGMENTSTORE_LOCATION" && -d "$AEM_SEGMENTSTORE_LOCATION_AZURE" ]]; then
    echo "not found"
fi

Upvotes: 4

Related Questions