Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用if-then语句进行帮助。Apple远程桌面和设置计算机信息字段_Bash - Fatal编程技术网

Bash 使用if-then语句进行帮助。Apple远程桌面和设置计算机信息字段

Bash 使用if-then语句进行帮助。Apple远程桌面和设置计算机信息字段,bash,Bash,这是一个非常适合网络管理员的脚本。它可以在计算机信息字段中设置运行脚本的计算机的AppleCare过期日期,以便在Apple Remote Desktop中轻松排序。我的问题是,当一台计算机的保修期到期时,我希望脚本将信息字段设置为“超出范围”。脚本按原样运行,我已经注释掉了我认为需要执行此操作的代码。任何帮助都将不胜感激。谢谢大家 #!/bin/bash # warranty.sh # Description: looks up Apple warranty info for # this

这是一个非常适合网络管理员的脚本。它可以在计算机信息字段中设置运行脚本的计算机的AppleCare过期日期,以便在Apple Remote Desktop中轻松排序。我的问题是,当一台计算机的保修期到期时,我希望脚本将信息字段设置为“超出范围”。脚本按原样运行,我已经注释掉了我认为需要执行此操作的代码。任何帮助都将不胜感激。谢谢大家

#!/bin/bash
# warranty.sh
# Description: looks up Apple warranty info for 
# this computer, or one specified by serial number 

# Based on a script by Scott Russell, IT Support Engineer, 
# University of Notre Dame
# http://www.nd.edu/~srussel2/macintosh/bash/warranty.txt
# Edited to add the ASD Versions by Joseph Chilcote
# Last Modified: 09/16/2010
# Edited 02/10/2011
# Updated support url 
# Added function to write data to plist

## Add function to cycle through a csv file

###############
##  GLOBALS  ##
###############

# make sure you use a full path
PlistLocal=".appwarranty.plist"
WarrantyTempFile="/tmp/warranty.txt"
AsdCheck="/tmp/asdcheck.txt"

if [[ $# == 0 ]] ; then
    SerialNumber=`system_profiler SPHardwareDataType | grep "Serial Number" | grep -v "tray" |  awk -F ': ' {'print $2'} 2>/dev/null`
else
    SerialNumber="${1}"
fi

[[ -n "${SerialNumber}" ]] && WarrantyInfo=`curl -k -s "https://selfsolve.apple.com/warrantyChecker.do?sn=${SerialNumber}&country=USA" | awk '{gsub(/\",\"/,"\n");print}' | awk '{gsub(/\":\"/,":");print}' | sed s/\"\}\)// > ${WarrantyTempFile}`

curl -k -s https://github.com/chilcote/warranty/raw/master/asdcheck -o ${AsdCheck} > /dev/null 2>&1


#################
##  FUNCTIONS  ##
#################

AddPlistString()
{
    # $1 is key name $2 is key value $3 plist location
    # example: AddPlistString warranty_script version1 /Library/ETC/appwarranty.plist
    /usr/libexec/PlistBuddy -c "add :"${1}" string \"${2}\"" "${3}"
}

SetPlistString()
{
    # $1 is key name $2 is key value $3 plist location
    # example: SetPlistString warranty_script version2 /Library/ETC/appwarranty.plist
    /usr/libexec/PlistBuddy -c "set :"${1}" \"${2}\"" "${3}"
}

GetWarrantyValue()
{
    grep ^"${1}" ${WarrantyTempFile} | awk -F ':' {'print $2'}
}
GetWarrantyStatus()
{
    grep ^"${1}" ${WarrantyTempFile} | awk -F ':' {'print $2'}
}
GetModelValue()
{
    grep "${1}" ${WarrantyTempFile} | awk -F ':' {'print $2'}
}
GetAsdVers()
{
    #echo "${AsdCheck}" | grep -w "${1}:" | awk {'print $1'}
    grep "${1}:" ${AsdCheck} | awk -F':' {'print $2'}
}

###################
##  CREATE PLIST ##
###################

if [[ ! -e "${PlistLocal}" ]]; then
    AddPlistString warrantyscript version1 "${PlistLocal}"
    for i in purchasedate warrantyexpires warrantystatus modeltype asd
    do
    AddPlistString $i unknown "${PlistLocal}"
    done
fi

###################
##  APPLICATION  ##
###################

echo "$(date) ... Checking warranty status"
InvalidSerial=`grep "wc.check.err.usr.pd04.invalidserialnumber" "${WarrantyTempFile}"`

###
# this is the part I'm struggling with
#
###
#
#   WarrantyStatus=`GetWarrantyStatus HW_SUPPORT_COV_SHORT`
#   echo "WarrantyStatus   ==  ${WarrantyStatus}"
#   SetPlistString warrantystatus "${WarrantyStatus}" "${PlistLocal}"
#
###
# if above is equal to "Out of Coverage" then 
# /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -computerinfo -set4 -4 "Out of Coverage"
# and stop
# Else do below
###

if [[ -e "${WarrantyTempFile}" && -z "${InvalidSerial}" ]] ; then

    WarrantyExpires=`GetWarrantyValue HW_END_DATE`
    echo "WarrantyExpires  ==  ${WarrantyExpires}"
    SetPlistString warrantyexpires "${WarrantyExpires}" "${PlistLocal}"

else
    if [[ -z "${SerialNumber}" ]]; then 
        echo "     No serial number was found."
        SetPlistString warrantystatus "Serial Not Found: ${SerialNumber}" "${PlistLocal}"
    fi
    if [[ -n "${InvalidSerial}" ]]; then
        echo "     Warranty information was not found for ${SerialNumber}."
        SetPlistString warrantystatus "Serial Invalid: ${SerialNumber}" "${PlistLocal}"
    fi
fi

####################################
##  MONTH CONVERSION & ARD ENTRY  ##
####################################

expdate=`date -j -f "%B %d, %Y" "${WarrantyExpires}" "+%Y-%m-%d"`
/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -computerinfo -set4 -4 "$expdate"

exit 0

看起来很简单。我假设
WarrantyStatus
变量就是用来保存这个字符串的东西:

if [[ "$WarrantyStatus" = "Out of Coverage" ]]; then
   .../kickstart -configure -computerinfo -set4 -4 "$WarrantyStatus"
   exit
fi

但是脚本将继续并尝试查找日期,即使它超出了覆盖范围,因此出错。如果发现您的语句为真,则脚本需要停止。如果它发现你的陈述是错误的,那么它需要继续。感谢Glenn的帮助。
exit
语句将停止脚本。关于“停止”的意思你有另一种想法吗?Rusty的评论是:“戴维,格伦已经回答了你的问题,并添加了代码。我也会考虑把这个添加到代码中,这个变量将激活/启动KICKSTEP。