Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

freddymoo

macrumors newbie
Original poster
Apr 9, 2015
4
0
hi all,

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

i have the following bash:

Code:
## 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. Unix or Linux Image

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!
 
Last edited by a moderator:
the conditional statement above works well if there's only 1 profile. Unix or Linux Image

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!
Edit/Delete Unix or Linux Message

I'm not sure I get what it is you are trying to do, if you want to check current logged in user against a list of users, you can use an array. But in this case, wouldn't you want to check for a match? If your current logged in user is in the list, then it pass, and if it's not found it exits.
 
I'm not sure I get what it is you are trying to do, if you want to check current logged in user against a list of users, you can use an array. But in this case, wouldn't you want to check for a match? If your current logged in user is in the list, then it pass, and if it's not found it exits.

Yes, correct. I'd like to check for a match. If the current logged in user is in the list, then it pass, and if it's not found it exits.

If userName is IN userCheck then pass Else fails.
 
Yes, correct. I'd like to check for a match. If the current logged in user is in the list, then it pass, and if it's not found it exits.

If userName is IN userCheck then pass Else fails.

Then you can't check for a non match, since the first non matching entry will exit. You could check a user name against the list, then set a flag to true if it's found in the list. Something like this would perhaps work.

Code:
#!/bin/bash

username="fred"
usercheck="fred fredmoo"
validuser=false

for i in ${usercheck[*]}
do
    if [ "$i" == "$username" ]
    then
        validuser=true
    fi
done

if [ "$validuser" == false ]
then
    echo "$username" is not a FileVault 2 enabled user.
    exit 1
fi
 
Code:
shopt -s extglob
if [ "$userName" = "" -o "${userName##@(${userCheck// /|})}" != "" ]; then
    echo "This user is not a FileVault 2 enabled user."
    exit 3
fi
 
Last edited by a moderator:
shopt -s extglob
if [ "${userName}" = "" -o "${userName#@(${userCheck// /|})}" != "" ]; then

If the current logged in user is in the list, it will still exit if there are more than one name in the list though.
 
Then you can't check for a non match, since the first non matching entry will exit. You could check a user name against the list, then set a flag to true if it's found in the list. Something like this would perhaps work.

Code:
#!/bin/bash

username="fred"
usercheck="fred fredmoo"
validuser=false

for i in ${usercheck[*]}
do
    if [ "$i" == "$username" ]
    then
        validuser=true
    fi
done

if [ "$validuser" == false ]
then
    echo "$username" is not a FileVault 2 enabled user.
    exit 1
fi


this might just work. i'll try it tomorrow. many thanks for your quick help! :)
 
If the current logged in user is in the list, it will still exit if there are more than one name in the list though.
If the current logged in user is in the list, "${userName#@(${userCheck// /|})}" = "" and it will not exit
 
Then you can't check for a non match, since the first non matching entry will exit. You could check a user name against the list, then set a flag to true if it's found in the list. Something like this would perhaps work.

Code:
#!/bin/bash

username="fred"
usercheck="fred fredmoo"
validuser=false

for i in ${usercheck[*]}
do
    if [ "$i" == "$username" ]
    then
        validuser=true
    fi
done

if [ "$validuser" == false ]
then
    echo "$username" is not a FileVault 2 enabled user.
    exit 1
fi

Many thanks! it worked :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.