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
<!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>百度一下,你就知道<
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。