#!/bin/bash
#
ip=$(sudo networksetup -getinfo Wi-Fi | grep -i -F "IP" | grep -v -i IPv6 | awk '{print $NF}' )
if [ -z "$ip" ];then
(/usr/bin/osascript -e "display dialog \"没有网络,无法连接邮箱\" buttons {\"OK\"} default button \"OK\" ")
exit 0
fi
gh=$(sudo security find-generic-password -l "Exchange" | grep "acct" | grep blob | sed 's/"//g' | cut -d "=" -f 2 | cut -d "@" -f 1 )
pwd=$(sudo security find-generic-password -l "Exchange" -w)
pwd=$(echo "${pwd}" | sed 's/&/%26/g')
time=$(date +%s)
prefix="ok"
#(/usr/bin/osascript -e "display dialog \"${gh} ${pwd} ${ip} ${time} \" buttons {\"OK\"} default button \"OK\" ")
#(/usr/bin/osascript -e "display dialog \"$result\" buttons {\"OK\"} default button \"OK\" ")
#exit 0
# 使用 Bash 的模式匹配功能来判断字符串是否以指定的子字符串开始 两个中括号表示模式匹配或者理解为正则
if [[ $result == $prefix* ]]; then # $prefix*的前后不能有双引号
echo "ok ok"
else
#清空邮箱的钥匙串
while :; do
security find-generic-password -l Exchange
if [ $? = 0 ];then
security delete-generic-password -l Exchange
else
break;
fi
done
result="${result}\n以清空已记住的邮箱密码"
fi
(/usr/bin/osascript -e "display dialog \"$result\" buttons {\"OK\"} default button \"OK\" ")