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>
            网页路径:<a href="http://bzwzjz.com/article/dipjge.html">http://bzwzjz.com/article/dipjge.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/ighihd.html">大数据开发中hive有什么用</a>
                </li><li>
                    <a href="/article/ighgoi.html">Dictionary中怎么批量插入日志数据</a>
                </li><li>
                    <a href="/article/ighgod.html">JavaScript如何实现的浏览器下载文件</a>
                </li><li>
                    <a href="/article/ighgse.html">8.31_Linux高级文件系统管理之磁盘配额、RAID和LVM的使用</a>
                </li><li>
                    <a href="/article/ighgog.html">python中ssl认证指的是什么</a>
                </li>        </ul>
    </div>
</div>
<div class="footer2">
    Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号 <br />友情链接:
    <a href="http://www.cxhljz.cn/" target="_blank">成都网站设计 </a><a href="http://chengdu.kswsj.cn/" target="_blank">高端网站设计 </a><a href="http://www.cdxwcx.cn/sheji/" target="_blank">网站设计公司 </a><a href="http://www.cxjianzhan.com/" target="_blank">网站制作公司 </a><a href="http://www.kswcd.com/" target="_blank">企业网站设计 </a><a href="http://chengdu.cdcxhl.com/" target="_blank">营销型网站建设 </a><a href="http://www.cqcxhl.com/service/ds.html" target="_blank">重庆电商网站建设 </a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">定制网站制作 </a><a href="http://www.cxhlcq.com/zhizuo/" target="_blank">重庆网站制作 </a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都网站设计 </a><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设 </a><a href="http://www.cdxwcx.cn/" target="_blank">成都网站制作公司 </a><a href="https://www.cdxwcx.com/" target="_blank">成都网站制作 </a><a href="https://www.cdxwcx.com/" target="_blank">成都网站设计 </a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">定制网站设计 </a><a href="http://www.cdweb.net/" target="_blank">成都网站建设 </a><a href="http://seo.cdkjz.cn/wangzhan/" target="_blank">营销型网站建设 </a><a href="http://m.cdcxhl.cn/H5/" target="_blank">成都响应式网站建设 </a><a href="http://www.kswcd.com/service/" target="_blank">高端网站建设 </a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">成都营销网站建设 </a><a href="http://www.cqcxhl.com/service/" target="_blank">品牌网站建设 </a><a href="http://www.cqwzjz.cn/" target="_blank">重庆网站建设 </a></div>

</body>
</html>
<script src="/Public/Home/js/wow.min.js"></script>
<script>
    if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))) {
        new WOW().init();
    };
</script>
<div class="sidebar">
    <ul>
        <li><a href="http://wpa.qq.com/msgrd?v=3&uin=244261566&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/right_qq.png" /></a></li>
        <li><a href="http://wpa.qq.com/msgrd?v=3&uin=1683211881&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/qq.png" /></a></li>
        <li class="tel"><a href="tel:028-86922220"><img src="/Public/Home/images/right_tel.png" /></a></li>
        <div class="wx">
            <span class="weixin"><img src="/Public/Home/images/weixin.jpg"><br> 微信扫一扫在线咨询</span>
        </div>
        <li><a class="fx" href="#hero"><img src="/Public/Home/images/right_up.png" /></a></li>
    </ul>
</div>
<script type="text/javascript">
    $(function () {
        $('.sidebar .fx').click(function () {
            $('html,body').animate({
                scrollTop: '0px'
            }, 800);
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#fancybox-manual-b").click(function () {
            $.fancybox.open({
                href: 'map.html',
                type: 'iframe',
                padding: 5
            });
        })
    });
</script>
<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>