shell script

Discussion in 'Mac Programming' started by freddymoo, Apr 9, 2015.

  1. freddymoo, Apr 9, 2015
    Last edited by a moderator: Apr 9, 2015

    freddymoo macrumors newbie

    Joined:
    Apr 9, 2015
    #1
    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!
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    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.
     
  3. freddymoo thread starter macrumors newbie

    Joined:
    Apr 9, 2015
    #3
    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.
     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    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
    
     
  5. dmi, Apr 9, 2015
    Last edited by a moderator: Apr 9, 2015

    dmi macrumors regular

    Joined:
    Dec 21, 2010
    #5
    Code:
    shopt -s extglob
    if [ "$userName" = "" -o "${userName##@(${userCheck// /|})}" != "" ]; then
        echo "This user is not a FileVault 2 enabled user."
        exit 3
    fi
     
  6. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    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.
     
  7. freddymoo thread starter macrumors newbie

    Joined:
    Apr 9, 2015
    #7

    this might just work. i'll try it tomorrow. many thanks for your quick help! :)
     
  8. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #8
    If the current logged in user is in the list, "${userName#@(${userCheck// /|})}" = "" and it will not exit
     
  9. freddymoo thread starter macrumors newbie

    Joined:
    Apr 9, 2015
    #9
    Many thanks! it worked :)
     

Share This Page