简体   繁体   中英

Bash script for macOS Loop if then else

Trying to do a loop with this script below. It works if I break it by deleting Google Chrome from Applications. If I put Chrome back into place it continuously kills the dock and says No matching processes belonging to you were found. Do I have to add something to killall Dock to exit the script or is Done in the wrong spot? Tried multiple things without any luck. Eventually want it to try every 15 minutes until all apps are in Applications and kill the dock so the shortcuts show instead of Question marks. This will happen once all apps get installed and Dock gets restarted.

i=0
while [ $i -lt 4 ]
do
if [[ -d "/Applications/Company Portal.app" && -d "/Applications/Google Chrome.app" ]];
        then
                killall Dock         
        else
i=$((i + 1)) | echo "Will tray again...."
sleep 10
fi
done

Update Here is what I eventually came up with, yea messy. But works! Will have to look at it more after see the responses here though.

echo $(date)

# Check to see if script has already run on computer before, if so, then exit.
file=/Users/Shared/.If_Installed_Restart_Dock_Has_Run.txt
if [ -e "$file" ]; then
    echo "The If_Installed_Restart_Dock.sh script has run before and exiting..." && exit 0
else
touch /Users/Shared/.If_Installed_Restart_Dock_Has_Run.txt
fi

i=0
while [ $i -lt 6 ]
do

if [[ -d "/Applications/Google Chrome.app" && -d "/Applications/Microsoft Edge.app" ]];
        then
                killall Dock && exit 0
        else
                echo "Applications still need installed in order to restart Dock, will check again in 10 minutes for up to an hour, intune will try again in 8hrs..."
fi

i=$((i + 1))
sleep 600
done

Chrome is irrelevant. You're falling victim to the classic blunder. Consider:

#!/bin/bash

i=0
while [ $i -lt 4 ]; do
        if echo "in first if, i = $i"; false ; then
                echo bar
        else
                echo "in else, i = $i"
                i=$((i + 1)) | echo "Will tray again...."  # (1)
                echo "after echo i = $i"
                i=$((i + 1))
                echo "after 2nd echo i = $i"
        fi
done

In the above code, the i=$((i + 1)) in line (1) does not increment the variable used in the control loop. Since that command is in a pipe, it is executed in a subshell, and the variable i in the main shell is not incremented. It would probably be better to structure your code as:

#!/bin/sh

i=0
while [ $((i++)) -lt 4 ]; do
        if [ -d "/Applications/Company Portal.app" ] && [ -d "/Applications/Google Chrome.app" ]; then
                killall Dock
        else
                echo "Will tray again...."
                sleep 10
        fi
done

or

#!/bin/bash

for (( i = 0; i < 4; i++ )); do
        if [ -d "/Applications/Company Portal.app" ] && [ -d "/Applications/Google Chrome.app" ]; then
                killall Dock
        else
                echo "Will tray again...."
                sleep 10
        fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM