网站建设资讯

NEWS

网站建设资讯

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://cdweb.net/article/dipjge.html">http://cdweb.net/article/dipjge.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dgijihe.html">jcqx在电脑上是哪一个快捷键的简单介绍</a>
                </li><li>
                    <a href="/article/dgijgci.html">android开发零基础 android开发从零开始</a>
                </li><li>
                    <a href="/article/dgijgsg.html">css去默认样式 css默认样式的优先级</a>
                </li><li>
                    <a href="/article/dgijgoc.html">ios16开发者模式在哪里 ios开发者设置在哪里</a>
                </li><li>
                    <a href="/article/dgijgsi.html">mysql在程序中怎么用 mysql如何使用</a>
                </li>        </ul>
    </div>
</div>
</div>
<footer>
    <div class="footop">
        <div class="wrap">
            <div class="bottomrpw">
                <div class="erp arp">
                    <dl>
                        <dt>ADDRESS</dt>
                        <dd class="address"> <i class="icon"></i> <span class="word">成都市青羊区锦天国际1号楼1002室</span> </dd>
                    </dl>
                </div>
                <div class="erp arp">
                    <dl>
                        <dt>TEL</dt>
                        <dd class="phonum"> <i class="icon"></i> <span class="word en"> <a href="tel:18980820575">18980820575</a> </span> </dd>
                    </dl>
                </div>
                <div class="erp crp">
                    <dl>
                        <dt>OTHER</dt>
                        <dd> <a class="word get-quote">获得报价与方案</a> </dd>
                        <dd> <a href="#" target="_blank" rel='nofollow' class="word" title="付款方式">付款方式</a> </dd>
                    </dl>
                </div>
                <div class="erp code-rp">
                    <dl>
                        <dt>Wechat</dt>
                        <dd class="code-wrap"> <span class="code"> <img src="/Public/Home/images/qr-code.jpg" alt="快上网微信公众号" /> </span> </dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
    <div class="footerbot">
        <div class="friendlinks">
            <div class="wrap">
                <ul class="rpl">
                    <li><a href="http://www.teliergzn.com/" title="特丽尔硅藻泥" target="_blank">特丽尔硅藻泥</a></li><li><a href="https://www.cdxwcx.com/400/" title="成都400电话" target="_blank">成都400电话</a></li><li><a href="http://www.gawzjs.com/" title="广安网站设计" target="_blank">广安网站设计</a></li><li><a href="http://www.schzyjs.com/" title="和正源建设" target="_blank">和正源建设</a></li><li><a href="http://www.cdkjz.cn/wangzhan/" title="网络推广公司" target="_blank">网络推广公司</a></li><li><a href="http://www.ruijiemsc.com/" title="瑞杰管理" target="_blank">瑞杰管理</a></li><li><a href="http://www.cdhuace.com/fuwu/huace.html" title="成都画册快印" target="_blank">成都画册快印</a></li><li><a href="https://www.cdcxhl.cn/
" title="php主机" target="_blank">php主机</a></li><li><a href="https://www.cdxwcx.com/cloud/" title="成都云服务器" target="_blank">成都云服务器</a></li><li><a href="http://chengdu.cdcxhl.com/weixin/" title="成都微信开发" target="_blank">成都微信开发</a></li>                </ul>
            </div>
        </div>
        <div class="wrap">
            <div class="copyright"> <span class="en">©2007-2022</span> 成都快上网科技有限公司 <span class="en">ALL RIGHTS
                        RESERVED.</span> <a rel="nofollow" href="http://www.miitbeian.gov.cn" target="_blank">蜀ICP备19037934号</a> </div>
        </div>
    </div>
</footer>
<div class="fcwrap">
    <ul class="rpl clearfix">
        <li class="phone"> <a rel="nofollow" target="_blank" href="tel:18980820575"> <i class="icon"></i>
            <strong>18980820575</strong> </a> </li>
        <li class="qq"> <a rel="nofollow" target="_blank"
                           href="https://wpa.qq.com/msgrd?v=1&uin=244261566&site=qq&menu=yes"> <i class="icon"></i>
            <strong>244261566</strong> </a> </li>
        <li class="back-top"> <a href="javascript:void(0)" rel="nofollow" class="back-to-top"> <i class="icon"></i>
            <strong>回到顶部</strong> </a> </li>
    </ul>
</div>
<!--nav-->
<div class="n-Wrap">
    <div class="navBar visble show">
        <div class="barlogo">
            <a href="/" rel="nofollow">
                <img src="/Public/Home/images/logo1.png" alt="成都做网站" />
                <img src="/Public/Home/images/logo2.png" alt="成都网站设计" />
            </a>
        </div>
        <div class="bmenu">
            <i class="bar-top"><span></span></i>
            <i class="bar-cen"><span></span></i>
            <i class="bar-bom"><span></span></i>
            <i class="bar-left"><span></span></i>
            <i class="bar-right"><span></span></i>
        </div>
    </div>
    <section class="fixmenu">
        <div class="close-bar">
            <i class="bar-left"><span></span></i>
            <i class="bar-right"><span></span></i>
        </div>
        <nav class="smph">
            <ul>
                <li class="index-hrefs on"><a href="http://www.cdweb.net/"><font>首页</font></a></li>
                <li><a href="/about/" rel="nofollow"><font>关于快上网</font></a></li>
                <li><a href="/service/" rel="nofollow"><font>服务范围</font></a></li>
                <li><a href="/case/" rel="nofollow"><font>案例展示</font></a></li>
                <li><a href="/solve/" rel="nofollow"><font>解决方案</font></a></li>
                <li><a href="/news/" rel="nofollow"><font>建站资讯</font></a></li>
                <li><a href="/contact/" rel="nofollow"><font>联系快上网</font></a></li>
            </ul>
            <div class="pwrap">
                <span class="label">服务热线</span>
                <strong class="phone"><a href="tel:18980820575">18980820575</a></strong>
            </div>
        </nav>
    </section>
</div>
<!--end nav-->
<script src="/Public/Home/js/hotcss.js"></script>
<script type="text/javascript" src="/Public/Home/js/su_new.js"></script>
</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>