Smogshaik
Smogshaik

Reputation: 345

How to check if two devices are mounted in macOS?

I want two external HDs to synchronize as soon as they are both connected at the same time.

I installed LaunchControl which will run a bash script as soon as something is mounted. The script should check if there are two devices called foo and bar and only then do something.

This is what I have:

if mount | grep "/Volumes/foo" > /dev/null; then
    echo "One is connected"
fi

Every attempt to add a second check for bar failed. This is what I tried:

if mount | [grep "/Volumes/foo" > /dev/null] && [grep "/Volumes/bar" > /dev/null]; then
    echo "Both are connected"
fi

Upvotes: 1

Views: 57

Answers (1)

Barmar
Barmar

Reputation: 782166

Your working code doesn't have [] around the call to grep, why did you think you needed to add that when doing multiple tests?

if mount | grep "/Volumes/foo" > /dev/null && mount | grep "/Volumes/bar" > /dev/null; then
    echo "Both are connected"
fi

Instead of redirecting to /dev/null you can use the -q option to grep.

if mount | grep -q "/Volumes/foo" && mount | grep -q "/Volumes/bar"; then
    echo "Both are connected"
fi

Upvotes: 1

Related Questions