【问题标题】:Why does a bash script continue running even though a dialog box is displayed on the screen waiting for an input from the user?为什么即使屏幕上显示一个对话框等待用户输入,bash 脚本仍会继续运行?
【发布时间】:2013-09-02 20:46:48
【问题描述】:

我有一个在 Mac OS X(X.5 到 X.8)机器上使用的 bash 脚本。在这是一个对话框情况,要求通过按“确定”继续脚本或通过按“打盹”让脚本打盹。那部分正在工作。

但是,我正在测试脚本并且无法立即按下任一按钮,并且在一两分钟后(我没有计时),脚本继续执行其余过程,让对话框停留在屏幕。

我的印象是脚本必须等待用户输入?

部分脚本:

osascript -e '告诉应用“Finder”激活'

return=osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon caution'

    ############  BEGIN LOOP HERE  ##############

while [ "$return" == "button returned:Snooze" ]

        do
        Runs every 4 hours
        sleep 14400
        osascript -e 'tell app "Finder" to activate'
        return=`osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon 2'`

完成

###### 在这里结束循环
    if [ "$return" == "button returned:OK" ]
        then
                    run the installer script here
            fi

【问题讨论】:

    标签: macos bash dialog osascript


    【解决方案1】:

    尝试将 return 语句的整个右侧放在反引号中,这将执行 osascript 并将“return”设置为该程序返回的字符串:

    return=`osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon caution'`
    

    否则你只是设置返回到'='右侧的字符串。

    【讨论】:

    • 我仔细检查了,反引号在那里。我试过下面的脚本,它也有同样的问题。那么,它一定是bash的一个功能吗?当我运行以下脚本时,对话框会弹出并停留在那里。但是脚本会在必要的 2 分钟后继续并关闭,而不是等待用户单击“确定”。 osascript -e '告诉应用程序“Finder”显示对话框“Text here”按钮 {“OK”} 默认按钮 1,标题为“title here”,图标停止' shutdown -r +2 >./test 2>&1 >./测试2
    • 更多信息:大约 2 分钟后,终端显示此错误“21:218: execution error: Finder got an error: AppleEvent timed out. (-1712)”。然后脚本继续。
    【解决方案2】:

    要忽略超时错误,您可以试试这个。 我们需要一个脚本来提示用户在密码到期前 14 天或更短的时间内更改密码。如果他们忽略提示,则脚本将因退出 0 而终止。您可以根据需要编辑此行为,但关键是如果除了我的条件测试"button returned:OK" 之外返回了 anything else该脚本将跳过我不希望它运行的内容

    if [ $daysRemaining -lt $daysBeforeExpire ]
        then
            echo "$userName has $daysRemaining day(s) remaining to change their password"
            echo "Informing User..."
            return=`osascript -e "tell application \"System Events\" to display dialog \"You have $daysRemaining day(s) to change your password... Logout and change password now?\" buttons {\"Later\",\"OK\"}"`
            if [[ "button returned:OK" = $return ]]
                then 
                echo "Returned OK"
                echo "Asking to log out."
                osascript -e "tell application \"System Events\" to log out"
            else
                echo "Returned Later - Skipping user log out"
                exit 0
            fi
        else
            echo "$daysRemaining day(s) remaining to change password"
    fi
    

    所以在你的情况下,这应该可以工作

        if [[ "button returned:OK" = $return ]]
            then
                 run the installer script here
            else 
                 sleep 100 #sleep the script OR
                 exit 0    #end it
            fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-02
      • 2018-05-29
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多