Search code examples
bashloopsprofiles

Issue with all conditions (configuration profiles) being checked


Ok, so now I have it setup like this but still it is giving me a ; exit; logout

[Process completed]

when run through the terminal I know that there are configuration profiles that are not on my computer that should make the script continue or rather to keep looping. What is this not happening?

Thanks in advance....!

Here is what i have so far:

#!/bin/bash

profilesInstalled=`profiles -P|awk '/attribute/ {print $4}'`

while read line ; do

if [ "$line" = "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" ];then

echo "AD Binding is present"

elif [ "$line" = "1C94DAD1-5FC7-46CE-9E09-576841C15093" ];then

echo "Energy Saver is present"

elif [ "$line" = "A0E5B977-F0AF-44C9-8001-DA0511B702B8" ];then

echo "Finder is present"

elif [ "$line" = "5E9DE5BF-34E4-4A7F-AA29-461FB0631943" ];then

echo "FV2 Redirect is present"

elif [ "$line" = "9AE91C88-D1B2-4227-9E95-80F492DCAA11" ];then

echo "Login Window/Security and Privacy is present"

elif [ "$line" = "00000000-0000-0000-A000-4A414D460003" ];then

echo "MDM Profile is present"

elif [ "$line" = "5E85BBF0-3483-4C80-A1FC-70AF20F82E7C" ];then

echo "Restrictions is present"

elif [ "$line" = "E433D546-5502-4C3F-9E5F-4732ED1F0032" ];then

echo "SAC SUBCA-01 is present"

elif [ "$line" = "5C2AE16B-D4E9-4D15-B190-3CD7B28779E8" ];then

echo "SAC SUBCA-02 is present"

elif [ "$line" = "2C620A13-DF1E-4F6A-A32B-9FA3149F8A56" ];then

echo "SAC-CA-01 is present"

elif [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then

echo "Screensaver is present" 

elif [ "$line" = "396A9D84-A9CA-4575-8D09-C9F054B76AF7" ];then

echo "Spotlight is present"

elif [ "$line" = "E0138F02-9A15-47BD-8CA5-7D1D0985A1A6" ];then

echo "Workday Corp is present"
fi 

exit 0

done <<<"$profilesInstalled"

Solution

  • Some "meta" remarks first:

    • Please don't change your original question substantially, as that can invalidate existing answers (such as Etan Reisner's helpful answer)
      • Instead, add later changes to the original question and mark the changes as such.
      • If a different (follow-up) question arises, ask it as a separate, new question.
    • Please learn how to format your code properly - it was done for you, and you managed to destroy that formatting again with your later edits.
    • Please read about how to provide an MCVE (a Minimal, Complete, and Verifiable Example).

    Not doing these things:

    • makes it far less likely that you'll get the help you need.
    • makes your question and its answers less valuable to future readers.

    Here's a cleaned-up version of your code:

    # Helper function to determine a string's element index in an array.
    # SYNOPSIS
    #     indexOf needle "${haystack[@]}"
    # *Via stdout*, returns the zero-based index of a string element in an array of strings or -1, if not found.
    # The *return code* indicates if the element was found or not.
    indexOf() {
      local e ndx=-1
      for e in "${@:2}"; do (( ++ndx )); [[ "$e" == "$1" ]] && echo $ndx && return 0; done
      echo '-1'; return 1
    }
    
    # Define array of profile IDs, and parallel ID of profile names.
    # Note: in bash 4+, this could be handled more elegantly with a single
    #       associative array.
    profileIds=( F2CC78D2-A63F-45CB-AE7D-BF2221D41218 1C94DAD1-5FC7-46CE-9E09-576841C15093
                 A0E5B977-F0AF-44C9-8001-DA0511B702B8 5E9DE5BF-34E4-4A7F-AA29-461FB0631943
                 9AE91C88-D1B2-4227-9E95-80F492DCAA11 00000000-0000-0000-A000-4A414D460003
                 5E85BBF0-3483-4C80-A1FC-70AF20F82E7C E433D546-5502-4C3F-9E5F-4732ED1F0032
                 5C2AE16B-D4E9-4D15-B190-3CD7B28779E8 2C620A13-DF1E-4F6A-A32B-9FA3149F8A56
                 3B44AE14-E0CE-4621-BACF-1A9C3BA4A459 396A9D84-A9CA-4575-8D09-C9F054B76AF7
                 E0138F02-9A15-47BD-8CA5-7D1D0985A1A6 )
    profileNames=( "AD Binding"                        "Energy Saver"
                   "Finder"                            "FV2 Redirect"
                   "Login Window/Security and Privacy" "MDM Profile"
                   "Restrictions"                      "SAC SUBCA-01"
                   "SAC SUBCA-02"                      "SAC-CA-01"
                   "Screensaver"                       "Spotlight"
                   "Workday Corp" )
    
    # Feeding the list of installed profile IDs via a process
    # substitution (<(...)), loop over them and print their
    # respective names.
    while read -r line ; do
    
      # Find the line in the array of profile IDs and
      # print the corresponding name.
      if ndx=$(indexOf "$line" "${profileIds[@]}"); then
        echo "${profileNames[ndx]} is present"
      else
        echo "WARNING: Unknown profile: $line" >&2
      fi
    
    done < <(profiles -P | awk '/attribute/ {print $4}')
    

    As for why your code didn't loop:

    • You have an unconditional exit 0 statement in your loop, which means that the loop is always exited after the 1st line.
    • Due to using <<< to feed the list of profiles, you always get at least 1 line of input, because <<< appends a trailing newline to its input. If the input is empty, you'll get one iteration with an empty line.

    As for this message:

    ; exit; 
    logout
    
    [Process completed]
    

    It tells me two things:

    • You're on OSX, and you ran the script from Finder.
    • The script produced no output (if there had been output, it would have printed between the exit; and logout lines).

    When you run a script from Finder, the shell that is used to run the script exits after the script has run - and whether its Terminal window stays open or not depends on your Terminal preferences - in your case, the window stays open, but since the shell has exited, you can't interact with it anymore.

    Either way, your particular script should yield the same output, whether it is run from Terminal directly, or via Finder.