1. 程式人生 > >shell 測試URL 是否正常腳本

shell 測試URL 是否正常腳本

shell腳本 測試頁面是否正常腳本

題目:老男孩教育每日一題-2017年5月3日-寫一個腳本:創建一個函數,能接受兩個參數

題目要求

1)第一個參數為URL,即可下載的文件;第二個參數為目錄,即下載後保存的位置;
2)如果用戶給的目錄不存在,則提示用戶是否創建;如果創建就繼續執行,否則,函數返回一個51的錯誤值給調用腳本;
3)如果給的目錄存在,則下載文件;下載命令執行結束後測試文件下載成功與否;如果成功,則返回0給調用腳本,否則,返回52給調用腳本;


解答: 此題涉及函數,read讀入 傳參 if判斷 等等,腳本還不完善,僅作為記錄

[[email protected] scripts]# cat download.sh 
#!/bin/sh
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions ##加載系統函數庫
URL=$1  ##傳參
DIR=$2
if [ $# -ne 2 ];then   #判斷傳參個數
   action "sh $0" /bin/false
  echo "Warning:Lack parameter"    
  echo "USAGE: sh $0 WEB_URL DIR_PATH"
  exit 1
fi
download(){  ##定義函數
if [ ! -d $DIR ];then
  read -p "$DIR not exist need create?(y/n)" char #read讀入
  if [ "$char" = "y" ]   ##if判斷 字符串比較“”雙引號括起來 用=等號比較
     then               #整數比較 不用引號 可以用 -eq
     mkdir $DIR -p
     cd $DIR
     wget  $URL  &>/dev/null
       if [ $? -ne 0 ];then
       return "52"   #return 函數中的返回值,類似於exit
     fi
    else
     return "51"
   fi
fi
}
download  $URL $DIR  ##前面download是函數名;$URL位置是函數的第一個參數,也是腳本的第一個參數=$1
if [ $? -eq 0 ];then
       action "wget $URL" /bin/true
       else
       sleep 1
       action "wget $URL" /bin/false
       sleep 1
       exit 1
     fi


測試結果

[[email protected] scripts]# sh download.sh www.baidu  qqq
qqq not exist need create?(y/n)y
wget www.baidu                                             [FAILED]
[[email protected] scripts]# sh download.sh 
sh download.sh                                             [FAILED]
Warning:Lack parameter
USAGE: sh download.sh WEB_URL DIR_PATH
[[email protected]
/* */ scripts]# sh download.sh www.baidu.com qiuyuetao qiuyuetao not exist need create?(y/n)y wget www.baidu.com [ OK ] [[email protected] scripts]# cat qiuyuetao/index.html <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道<


本文出自 “逗哥筆記” 博客,請務必保留此出處http://qiuyt.blog.51cto.com/1229789/1921812

shell 測試URL 是否正常腳本