BenjiMan
BenjiMan

Reputation: 29

If/else Statement with macOS terminal

So I'm quite new to the macOS terminal and I would like to execute a simple command in which if the hidden mac folders are shown, hide them and if they are hidden, show them.

I'm mainly used to python so my first reflex would be:

if defaults write com.apple.finder AppleShowAllFiles is NO: 
    defaults write com.apple.finder AppleShowAllFiles YES 
else: 
    defaults write com.apple.finder AppleShowAllFiles NO

Now I'm pretty sure that this wouldn't work but how could I achieve something like this inside a shell script?

Upvotes: 1

Views: 12068

Answers (2)

Rajender Saini
Rajender Saini

Reputation: 614

To type if then else on terminal

if (( 1 == 1));then echo "hi"; fi;

Upvotes: 3

Michael Domino
Michael Domino

Reputation: 419

You could do something like this:

#!/bin/bash

if [ '1' = $(defaults read com.apple.finder AppleShowAllFiles) ]; then
    echo "AppleShowAllFiles is enabled"
elif [ '0' = $(defaults read com.apple.finder AppleShowAllFiles) ];   then
    echo "AppleShowAllFiles is not enabled"
else
    echo "defaults returned some other value"
fi

Or this to assign the return value of defaults to a variable:

#!/bin/bash

defaultsReturn=$(defaults read com.apple.finder AppleShowAllFiles)

if [ '1' = "$defaultsReturn" ]; then
    echo "AppleShowAllFiles is enabled"
elif [ '0' = "$defaultsReturn" ]; then
    echo "AppleShowAllFiles is not enabled"
else
    echo "defaults returned some other value: $defaultsReturn"
fi

Upvotes: 4

Related Questions