溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

shell 測試URL 是否正常腳本

發(fā)布時(shí)間:2020-06-28 09:52:48 來源:網(wǎng)絡(luò) 閱讀:3209 作者:邱月濤 欄目:開發(fā)技術(shù)

題目:老男孩教育每日一題-2017年5月3日-寫一個(gè)腳本:創(chuàng)建一個(gè)函數(shù),能接受兩個(gè)參數(shù)

題目要求

1)第一個(gè)參數(shù)為URL,即可下載的文件;第二個(gè)參數(shù)為目錄,即下載后保存的位置;
2)如果用戶給的目錄不存在,則提示用戶是否創(chuàng)建;如果創(chuàng)建就繼續(xù)執(zhí)行,否則,函數(shù)返回一個(gè)51的錯(cuò)誤值給調(diào)用腳本;
3)如果給的目錄存在,則下載文件;下載命令執(zhí)行結(jié)束后測試文件下載成功與否;如果成功,則返回0給調(diào)用腳本,否則,返回52給調(diào)用腳本;


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

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


測試結(jié)果

[root@db02 scripts]# sh download.sh www.baidu  qqq
qqq not exist need create?(y/n)y
wget www.baidu                                             [FAILED]
[root@db02 scripts]# sh download.sh 
sh download.sh                                             [FAILED]
Warning:Lack parameter
USAGE: sh download.sh WEB_URL DIR_PATH
[root@db02 scripts]# sh download.sh www.baidu.com qiuyuetao
qiuyuetao not exist need create?(y/n)y
wget www.baidu.com                                         [  OK  ]
[root@db02 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>百度一下,你就知道<


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI