OS X shell script

hi all,

i have 2 user profiles (fred, fredmoo) with filevault 2 enabled.

i have the following bash:

## Get the logged in user's name
userName=$(/usr/bin/stat -f%Su /dev/console)


## This first user check sees if the logged in account is already authorized with FileVault 2

userCheck=`fdesetup list | awk -v usrN="$userName" -F, 'index($0, usrN) {print $1}'`
if [ "${userCheck}" != "${userName}" ]; then
    echo "This user is not a FileVault 2 enabled user."
    exit 3
fi

----------------
if i do echo userName , i get -----> fred

if i do echo userCheck , i get -----> fred fredmoo
------------------

the conditional statement above works well if there's only 1 profile. :b:

however since my mac has more than 1 user profiles, the statement will echo "This user is not a FileVault 2 enabled user." and exit.

userCheck has both profiles.

how do i modify the if statement to say if userName does NOT equal to the 1st userCheck or 2nd userCheck, then echo "This user is not a FileVault 2 enabled user." and exit?

i would appreciate any assistance. thanks so much!

See if this works better:

awk -v usrN="$userName" -F, '$1==usrN {print $1}'

instead of

awk -v usrN="$userName" -F, 'index($0, usrN) {print $1}'

If the bash you use is sufficiently recent, it might provide the additional binary operator, =~ (c.f. man bash )

Try

[[ " $userCheck " =~ " $userName " ]] && echo in || echo out

but make sure the surrounding spaces are there. You might even end up doing sth like

[[ " $(fdesetup list) " =~ " $userName " ]] && echo in || echo out

sorry i'm a new beginner.

could you please help me where to put this code? many thanks

Did you test if it works at all?
Try

[[ ! " $(fdesetup list) " =~ " $userName " ]] && { echo "This user is not a FileVault 2 enabled user."; exit 3; }

Also, did you check the alternative awk suggestion? How did that work out?

--
@RudiC, fdsetup list produces a comma-separated record on every line, so when you put the whole command in a string, the first field of a record could be preceded by either a ^ or a newline...

So, perhaps more like so:

[[ ! $(fdesetup list) =~ (^|$'\n')"$userName", ]]