Shell脚本(curl)中的错误处理

时间:2018-06-29 02:30:57

标签: shell curl error-handling

如果下面脚本中的curl命令失败,我想实现某种形式的错误纠正。
如果它不能下载所需的文件,或者昨天的文件不可用,则应退出脚本。然后应将结果与前天的文件进行比较。

#!/bin/bash


echo "This script will check the following websites for changes"
echo "1. APAC"
echo "2. AMERICAS"
echo "3. EMEA"
#read -p "Press enter to continue"

echo -ne '#####                     (33%)\r'
sleep 1


URL1="https://support.symantec.com/en_US/article.TECH244698.html" #APAC
URL2="https://support.symantec.com/en_US/article.TECH243000.html" #AMERICAS
URL3="https://support.symantec.com/en_US/article.TECH244697.html" #EMEA

DATE=$(date +"%Y%m%d") #defines the date which is used in the logfile name
YESTERDAY=$(date --date="yesterday" +"%Y%m%d")


SAI1=$(diff -U4 logfiles/APAC_$YESTERDAY.log            logfiles/APAC_$DATE.log)          #DIFF COMMANDS
SAI2=$(diff -U4 logfiles/AMERICAS_$YESTERDAY.log        logfiles/AMERICAS_$DATE.log)
SAI3=$(diff -U4 logfiles/EMEA_$YESTERDAY.log            logfiles/EMEA_$DATE.log)

#This processes the 3 URLs from above, awk extracts the tables and creates a simplified html file with only the ip tables.
#XSLT processor produces the output and sed will remove lines that are not required

export https_proxy=https://XXXXXXX:XXXXXX@111.111.111.111:0000 #All connections hereafter use this proxy until line 'unset HTTPS_PROXY' is reached
                                                                 #Proxy is set as: https://LDAP:PW@IPorHOSTNAME:PORT

#GET IP LIST AND HTML FILES
curl --silent "$URL1" |\
 awk 'BEGIN{print "<html><body>"}/<table/{a=1;}/<\/table>/{print;a=0}{if(a)print;}END{print "</body></html>"}' |\
 xsltproc -html generate_ips.xslt - | sed '/^Egress/{d};s/^ *//' > logfiles/APAC_$DATE.log

curl --silent "$URL1" > logfiles/HTML_APAC_$DATE.log #Gets URL1 (APAC) as HTML file and saves it to specified filename

curl --silent "$URL2" |\
 awk 'BEGIN{print "<html><body>"}/<table/{a=1;}/<\/table>/{print;a=0}{if(a)print;}END{print "</body></html>"}' |\
 xsltproc -html generate_ips.xslt - | sed '/^Egress/{d};s/^ *//' > logfiles/AMERICAS_$DATE.log

curl --silent "$URL2" > logfiles/HTML_AMERICAS_$DATE.log #Gets URL2 (AMERICAS) as HTML file and saves it to specified filename

echo -ne '#############             (66%)\r'
sleep 1

curl --silent "$URL3" |\
 awk 'BEGIN{print "<html><body>"}/<table/{a=1;}/<\/table>/{print;a=0}{if(a)print;}END{print "</body></html>"}' |\
 xsltproc -html generate_ips.xslt - | sed '/^Egress/{d};s/^ *//' > logfiles/EMEA_$DATE.log

curl --silent "$URL3" > logfiles/HTML_EMEA_$DATE.log #Gets URL3 (EMEA) as HTML file and saves it to specified filename


sleep 1 #Wait for 1sec

#DIFF and WRITE OUT TO FILE
if [[ $SAI1 -eq 0 ]]
then
 echo $"APAC IP List: No Change detected" | tee  logfiles/APAC_diffresult_$DATE
else
 echo -n "$SAI1" | tee logfiles/APAC_diffresult_$DATE
fi

if [[ $SAI2 -eq 0 ]]
then
 echo $"AMERICAS IP LIST: No Change detected" | tee  logfiles/AMERICAS_diffresult_$DATE
else
 echo -n "$SAI2" | tee logfiles/AMERICAS_diffresult_$DATE
fi

if [[ $SAI3 -eq 0 ]]
then
 echo $"EMEA IP LIST: No Change detected" | tee  logfiles/EMEA_diffresult_$DATE
else
 echo -n "$SAI3" | tee logfiles/EMEA_diffresult_$DATE
fi

sleep 1 #Wait for 1 sec

#Mail Notification
cat logfiles/APAC_diffresult_$DATE      | mail -s "APAC IP LIST UPDATE" user@mail.com
cat logfiles/AMERICAS_diffresult_$DATE  | mail -s "AMERICAS IP LIST UPDATE" user@mail.com
cat logfiles/EMEA_diffresult_$DATE      | mail -s "EMEA IP LIST UPDATE" user@mail.com

sleep 1

unset HTTPS_PROXY #Disables proxy

sleep 1

echo -ne '#######################   (100%)\r'
echo -ne '\n'

sleep 1

exit 0; #Finish

0 个答案:

没有答案