Title: The old boy Education Day-May 3, 2017-Write a script: Create a function that can accept two parameters of the topic requirements
1) The first parameter is a URL, you can download the file, the second parameter is a directory, that is, the location of the download after the save;
2) If the user-given directory does not exist, the user is prompted to create it, or if it is created, the function returns a 51 error value to the calling script;
3) If the given directory exists, then download the file; Download command after the execution of the test file download success or not; If successful, return 0 to the calling script, otherwise, return 52 to the calling script;
Answer: This question involves the function, read reads into the parameter if judgment and so on, the script is not perfect, only as the record
[[email protected] scripts]# cat download.sh #!/bin/sh[ -f /etc/init.d/ functions ]&& . /etc/init.d/functions # #加载系统函数库URL =$1 # #传参DIR =$2if [ $# -ne 2 ];then #判断传参个数 action "sh $0" /bin/false echo "Warning:lack parameter" echo "Usage: sh $0 web_url dir_path" exit 1fidownload () { ## Define functions if [ ! -d $DIR ];then read -p $DIR not exist need create? (y/n) " char #read读入 if [ $char = " y " ] # #if判断 string comparison "" enclosed in double quotes by = equals sign then #整数比较 No quotes can be used -eq mkdir $DIR -p cd $DIR wget $URL &>/dev/null if [ $? -ne 0 ];then return "" " #return The return value in the function, Similar to exit fi else return " Wuyi " fifi}download $URL $DIR # #前面download是函数名; $URL position is the first parameter of the function, is also the first parameter of a script =$1if [ $? -eq 0 ];then action "wget $URL" /bin/true else sleep 1 action "wget $URL" /bin /false sleep 1 exit 1 fi
Test results
[[Email protected] scripts]# sh download.sh www.baidu qqqqqq not exist need create? (y/n) ywget www.baidu [failed][[email protected] scripts]# sh download.sh sh download.sh [failed]warning:lack parameterusage: sh download.sh web_url dir_path[[ email protected] scripts]# sh download.sh www.baidu.com qiuyuetaoqiuyuetao&Nbsp;not exist need create? (y/n) ywget www.baidu.com [ ok ][[email protected ] scripts]# cat qiuyuetao/index.html <! doctype html><!--status ok-->
This article is from the "funny Brother Notes" blog, be sure to keep this source http://qiuyt.blog.51cto.com/1229789/1921812
Shell Test URL is normal script