osx - Find FileVault2 user in fdesetup output with Awk -
i have 2 user profiles (fred, fredmoo) filevault 2 enabled.
i have following bash:
## logged in user's name username=$(/usr/bin/stat -f%su /dev/console)
this first user check sees if logged in account authorized filevault 2
usercheck=`fdesetup list | awk -v usrn="$username" -f, 'index($0, usrn) {print $1}'` if [ "${usercheck}" != "${username}" ]; echo "this user not filevault 2 enabled user." exit 3 fi
if echo $username
, get
fred
if echo $usercheck
, get
fred fredmoo
the conditional statement above works if there's 1 profile. unix or linux image, since mac has more 1 user profile, statement echo "this user not filevault 2 enabled user." , exit.
usercheck has both profiles.
how modify if statement if username not equal 1st usercheck or 2nd usercheck, echo "this user not filevault 2 enabled user." , exit?
the output fdesetup
list
looks this:
fredmoo,485a09cf-b0d5-469a-8224-2dd1877e780b administrator,ddb87e8d-8150-4d06-a59d-774ad28d119c gollum,8ae6c365-e38f-49e2-998c-f4742cc9980c
your awk script looks fred
anywhere in fdesetup
output, of course finds when output contains fredmoo
.
you seem have comma-separated output comparing against, awk script should awk -v user="$(whoami)" -f , '$1 == user { print $1 }'
(where assume output looks in sample here: https://derflounder.wordpress.com/2013/10/22/managing-mavericks-filevault-2-with-fdesetup/).
also, common antipattern storing stuff need once in variable, complicates things , pollutes namespace. try change awk script sets correct exit code; can use directly in conditional. maybe refactor reusable function.
warn () { echo "$0: $@" >&2 } die () { rc=$1 shift warn "$@" exit $rc } canihazfv2 () { fdesetup list | awk -v user="$1" -f , '$1 == user { print $1; exit 0 } end { exit 1 }' } me=$(whoami) canihazfv2 "$me" || die 3 "$me not enabled use filevault 2"
notice how script identifies in error message (so when build new scripts call other scripts call script, can see 1 failing 3 years now) actual input triggered error, , prints error message standard error through redirection.
as always, this || that
shorthand if ! this; that; fi
of course longhand might preferable if that
moderately complex (which avoid here encapsulating die
in function).