While loop counter help

cprimero
New Contributor III

Having some difficulty figuring this one out. I am looking to add a counter to my While loop within Bash. I have /usr/bin/profiles checking to see if a configuration profile has made it on to the machine before continuing on. This config profile will make its way to the machine after the script removes a cert in key chain which in turn will remove it from a smart group via recon. After this action, it will become relevant to take on the config profile. This while loop on the other hand will wait for the config profile to come down, but I would like it to time out after say... 20 loops.

Here is the snippet (sensitive info will be subbed out)

#!/bin/sh


Profile=B2173820-3729-2819-2839-389283382192
lookUp=$(usr/bin/profiles -C | grep "${Profile}")


while [[ "$lookUp" == "" }}; do
          echo "Profile does not exist, will continue to check"
          sleep 5
          lookUp=$(usr/bin/profiles -C | grep "${Profile}")
 done

While loop works as is. Any suggestions on adding a counter? I have tried a few ways, but I have a feeling I am just not grasping on the concept of it.

1 ACCEPTED SOLUTION

stevewood
Honored Contributor II

There's no need to write the counter out to a file since you are staying in the script. A simple variable to store the count with an if/then/else statement inside the while loop shod suffice. Something like:
.

counter=0
while something; do
If [[ $counter -lt 20 ]]; then
((counter++))
lookUp
else
echo "run 20 times"
exit 1
fi
done

Typing this on my phone so pardon the pseudo code but you should get the idea.

View solution in original post

4 REPLIES 4

trippmcc
New Contributor

I pieced this together from another script for (OS Software Update deferrals) but the functionality should work.

I did a quick test and it seemed to work ok for me to the extend I could test it.

Update: After thinking about it more reworked the script so it runs automatically. Which I believe may be what you were originally asking for.

#!/bin/bash

Profile=B2173820-3729-2819-2839-389283382192

lookUp=$('/usr/bin/profiles' -C | grep "${Profile}")


while [[ "$lookUp" == "" ]]; 

do
        counterpath="/Library/Application Support/JAMF/com.example.osupdatedeferral.plist"

        sleep 5

        #check if counter file exists. If it does, increment the count and store it
        if [ -f "$counterpath" ]; then
            echo "Counter file found."
            count=`defaults read "$counterpath" DeferralCount`
            echo "Old count is $count"
            ((count++))
            echo "New count is $count"
            defaults write "$counterpath" DeferralCount -int $count

        #if the counter file is not found, create one with count 0
        else 
            echo "Counter file does not exist. Creating one now."
            defaults write "$counterpath" DeferralCount -int 0
            count=0
            echo "Count is $count"
        fi

        if [ "$count" -lt 20 ]; then

          echo "Profile does not exist, will continue to check"
          lookUp=$('/usr/bin/profiles' -C | grep "${Profile}")

        else
            echo "The script has looped more than 20 times"
                        exit 1
        fi
done

cprimero
New Contributor III

@tripmcc

Thank you I think that clears it up in my mind. Is there a way to utilize a counter without having to store the count in a file?

stevewood
Honored Contributor II

There's no need to write the counter out to a file since you are staying in the script. A simple variable to store the count with an if/then/else statement inside the while loop shod suffice. Something like:
.

counter=0
while something; do
If [[ $counter -lt 20 ]]; then
((counter++))
lookUp
else
echo "run 20 times"
exit 1
fi
done

Typing this on my phone so pardon the pseudo code but you should get the idea.

View solution in original post

cprimero
New Contributor III

@stevewood @trippmcc

Tried this yesterday and I was missing the "((counter++)), the portion that actually makes it tick up! ah it all comes full circle now.
Thank you both.