Reputation: 29
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
Reputation: 614
To type if then else on terminal
if (( 1 == 1));then echo "hi"; fi;
Upvotes: 3
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