0

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"
mklement0
  • 382,024
  • 64
  • 607
  • 775
dondo
  • 3
  • 4
  • The `if` in `if [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then` must be `elif` - also note that your `while` loop exits unconditionally after the 1st iteration, and that it implicitly reads from _stdin_ - you probably wanted `done <<<"$profilesInstalled"`. – mklement0 Apr 17 '15 at 03:33

2 Answers2

1

You need a space around the = in those tests. That first test will always pass as written.

You should also quote the "$line" variable expansion.

Unless you use $profilesInstalled somewhere else you don't need that variable at all and can just pipe the profiles pipeline to the while loop directly.

You can also replace grep in that pipeline with awk '/attribute/ {print $4}'.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Thanks I have added your suggestions and have made sure that some of the configuration profiles are absent to make sure the script is doing what it is supposed to do. Unfortunetlyit is still not working properly and I am getting a syntax error: line 34: syntax error near unexpected token `done' – dondo Apr 17 '15 at 01:49
  • Here is the script so far. Had to shorten it to fit #!/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" fi exit 0 done – dondo Apr 17 '15 at 01:52
  • @dondo Entirely unreadable like that. Add the new code to the original post (don't delete the original code) and include the exact error. – Etan Reisner Apr 17 '15 at 02:05
  • 1
    @mklement0 Adding that comment on the question is likely a better idea. – Etan Reisner Apr 17 '15 at 03:31
0

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.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Wow, this is amazing. Thank you so much! My apologies for not formating things properly and deleting things. I am new here. I will definitely follow up on how to provide an MCVE. I am going to study what you have put together so I can get a better understanding of it. Thanks again! – dondo Apr 18 '15 at 06:46
  • @dondo: My pleasure; glad to hear it. Another tip worth mentioning: [shellecheck.net](http://shellcheck.net) allows you to syntax-check your shell code. – mklement0 Apr 18 '15 at 18:42