shell测试URL是否正常脚本-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

shell测试URL是否正常脚本-创新互联

题目:老男孩教育每日一题-2017年5月3日-写一个脚本:创建一个函数,能接受两个参数

题目要求

1)第一个参数为URL,即可下载的文件;第二个参数为目录,即下载后保存的位置;
2)如果用户给的目录不存在,则提示用户是否创建;如果创建就继续执行,否则,函数返回一个51的错误值给调用脚本;
3)如果给的目录存在,则下载文件;下载命令执行结束后测试文件下载成功与否;如果成功,则返回0给调用脚本,否则,返回52给调用脚本;

专注于为中小企业提供网站设计制作、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业宛城免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

解答: 此题涉及函数,read读入 传参 if判断 等等,脚本还不完善,仅作为记录

[root@db02 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

测试结果

[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 

 百度一下,你就知道<</pre><p></p><p align=center>另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、<a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防服务器</a>、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。</p>            <br>
            当前标题:shell测试URL是否正常脚本-创新互联            <br>
            URL链接:<a href="http://kswsj.cn/article/dipjge.html">http://kswsj.cn/article/dipjge.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/sgcjjs.html">代运营抖音账号账号服务咨询(抖音账号运营公司排名前十)</a>
                </li><li>
                    <a href="/article/sgcjgi.html">大健康产品,抖音账号私域运营四大方法</a>
                </li><li>
                    <a href="/article/sgcjig.html">株洲短视频运营职业学校</a>
                </li><li>
                    <a href="/article/sgcjje.html">生产制造抖音代运营怎么选</a>
                </li><li>
                    <a href="/article/sgcjgp.html">短视频运营软件价格分析</a>
                </li>        </ul>
    </div>
</div>
<div class="line"></div>
<!--底部-->
<footer id="5">
    <div class="foot1 container">
        <div class="list">
            <div class="item">
                <a href="javascript:;">
                    <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span>
                    <strong>关注我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>索要报价</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>我要咨询</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>找到我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>投诉建议</strong>
                </a>
            </div>
        </div>
        <div class="tel">
            <dl>
                <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br>
                <span>也许您需要专业的服务,欢迎来电咨询</span>
            </dl>
            <dl>
                <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br>
                <span>您的需求,是我们前进的动力</span>
            </dl>
        </div>
    </div>
    <div class="friend">
        <div class="container">
            <span class="tit">友情链接:</span>
            <div class="inner">
                <a href="https://www.cdcxhl.com/tuoguan/yaan/" target="_blank">雅安服务器托管</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站设计公司</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销推广</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音代运营</a><a href="https://www.cdcxhl.com/" target="_blank">网站建设公司</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发稿投放</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站设计</a><a href="https://www.cdcxhl.com/ddos/" target="_blank">DDOS防护</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">分类目录网站</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租用</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销型网站建设</a><a href="https://www.cdcxhl.com/link/" target="_blank">买卖链接</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">成都外贸网站建设公司</a><a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站设计公司</a><a href="https://www.cdcxhl.com/link/" target="_blank">买友情链接</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://www.abwzjs.com/" target="_blank">阿坝网站建设</a><a href="http://www.cdkjz.cn/fangan/cosmetics/" target="_blank">化妆品企业网站建设方案</a><a href="http://m.cdcxhl.com/muban.html" target="_blank">成都模版网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdcxhl.com/idc/gylt.html" target="_blank">贵阳联通机房</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">机柜租用</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租赁</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作</a><a href="http://www.cxjianzhan.com/" target="_blank">成都网站制作</a><a href="http://www.cxjianzhan.com/" target="_blank">网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/service/guangdianxuke.html" target="_blank">广播电视节目制作许可证</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接购买</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a>            </div>
            <div class="fr ecode">
                <div class="fl">
                    <img src="/Public/Home/img/ewm.jpg">
                    <p>关注企业微信</p>
                </div>
                <div class="fr slogan">
                    <p class="icon">
                        <a class="ph" href=""><i class="iconfont"></i></a>
                        <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a>
                    </p>
                    <p>
                        <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、
                        <a href="">营销型网站建设</a>、<a href="">网站建设</a>、
                        <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:成都创新互联科技有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a>
                服务热线:028-86922220
            </p>
            <p class="fr">
                <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:
                <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a>
            </p>
        </div>
    </div>
</footer>
<!--在线咨询-->
<div class="fot">
    <ul>
        <li>
            <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank">
                <img src="/Public/Home/img/fot1.png" alt="">
                <p>在线咨询</p>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" target="_blank">
                <img src="/Public/Home/img/fot2.png" alt="">
                <p>拨打电话</p>
            </a>
        </li>
    </ul>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>