繁体   English   中英

macOS 循环的 Bash 脚本 if then else

[英]Bash script for macOS Loop if then else

试图用下面的这个脚本做一个循环。 如果我通过从应用程序中删除 Google Chrome 来破坏它,它会起作用。 如果我将 Chrome 放回原位,它会不断杀死扩展坞并显示未找到属于您的匹配进程。 我必须添加一些东西来 killall Dock 退出脚本还是在错误的位置完成? 在没有任何运气的情况下尝试了多种事情。 最终希望它每 15 分钟尝试一次,直到所有应用程序都在应用程序中并终止扩展坞,以便显示快捷方式而不是问号。 一旦安装了所有应用程序并重新启动 Dock,就会发生这种情况。

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

更新这是我最终想出的,是的,很乱。 但是有效! 不过,在看到这里的回复后,将不得不更多地关注它。

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

铬是无关紧要的。 你正在成为经典错误的牺牲品。 考虑:

#!/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

在上面的代码中,第 (1) 行中的i=$((i + 1)) (1)增加控制循环中使用的变量。 由于该命令位于 pipe 中,因此它在子 shell 中执行,并且主 shell 中的变量i不会递增。 将代码构造为:

#!/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

或者

#!/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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