/ / If / else Instrukcja z terminalem macOS - macos, terminal, macos-sierra

Jeśli / else Instrukcja z terminalem macOS - macos, terminal, macos-sierra

Tak więc jestem całkiem nowy w terminalu macOS i chciałbym wykonać proste polecenie, w którym jeśli ukryte foldery mac zostaną wyświetlone, ukryj je i jeśli są ukryte, pokaż je.

Jestem głównie używany do python, więc mój pierwszy refleks będzie:

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

Teraz jestem pewien, że to nie działa, ale jak mogę osiągnąć coś takiego w skrypcie powłoki?

Odpowiedzi:

0 dla odpowiedzi № 1

Możesz zrobić coś takiego:

#!/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

Lub to, aby przypisać wartość zwracaną wartości domyślnych do zmiennej:

#!/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