1、文件下载指令应用
支持断点续传
curl -C - -O "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"
curl -C - -O "https://curl.haxx.se/download/archeology/md5.txt"
直接覆盖
curl "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz" -o curl-7.58.0.tar.gz --progress
curl "https://curl.haxx.se/download/archeology/md5.txt" -o md5.txt --progress
系统自带的wget指令下载文件也类似
wget "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"
wget -O curl-7.58.0.tar.gz "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"
2、产品固件更新应用demo
upgrade.sh
#!/bin/sh url="https://curl.haxx.se/download/archeology/" while true do #1)download file md5_url=${url}"md5.txt" #curl md5_url -o md5.txt --progress wget -O md5.txt "$md5_url" version=$(sed -n 1p md5.txt) filename=$(sed -n 2p md5.txt) md5=$(sed -n 3p md5.txt) version_bak=$(sed -n 1p md5_bak.txt) echo $version echo $filename echo $md5 echo $version_bak #2)md5sum firmware if [[ "$version" != "$version_bak" ]];then cp md5.txt md5_bak.txt download_url=${url}${filename} download_url=${download_url%$' '} #curl "$download_url" -o $filename --progress wget -O $filename "$download_url" md5_calc=$(md5sum $filename|cut -d ' ' -f1) #3)replace app file---> reboot if [ "$md5"=="$md5_calc" ] ; then ./kill_app.sh #replace upgrade file ./run_app.sh rm $filename time=$(date "+%Y-%m-%d %H:%M:%S") echo "${time} upgrade $md5_calc $filename " >> log.txt else echo "upgrade file md5sum err !" fi else echo "local version is the latest version !" fi sleep 10 done
3、 md5.txt
md5.txt文件内置3行关键信息
7.58.0
curl-7.58.0.tar.gz
7e9e9d5405c61148d53035426f162b0a
4、curl安装
./configure --prefix=/usr/local/curl
make
sudo make install
export PATH=$PATH:/usr/local/curl/bin
export LD_LIBRARY_PATH=/usr/local/curl/lib:$LD_LIBRARY_PATH
5、curl移植
./configure --prefix=/home/dong/curl/curl-7.58.0/_install --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
make
sudo make install
6、 wget编译,安装
# tar zxvf wget-1.9.1.tar.gz
# cd wget-1.9.1
# ./configure
# make
# make install
7、progress
wget -c https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz -o load.log
progress.sh
#wget -c https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz -o load.log while true do sed -n '/..........>/p' load.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4,$5,$6,$7}' sed -n '/...>/p' load.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4}' sleep 1 done
8、progress.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> //类似wget的进度条的实现,实际就是转移符 的使用, 的作用是返回至行首而不换行 int main(int argc, char *argv[]) { unsigned len = 60; char *bar = (char *)malloc(sizeof(char) * (len + 1)); for (int i = 0; i < len + 1; ++i) { bar[i] = '#'; } for (int i = 0; i < len; ++i) { printf("progress:[%s]%d%% ", bar+len-i, i+1); fflush(stdout);//一定要fflush,否则不会会因为缓冲无法定时输出。 usleep(100000); //sleep(1); } printf(" "); return 0; }
9、checkurl.sh
#!/bin/bash usage(){ echo "Usage:$0 url" exit 1 } checkurl(){ local num=`curl -I -m 5 -s -w "%{http_code} " -o /dev/null $1 |egrep "(200|301|302)"|wc -l` if [ $num -eq 1 ] then echo "ok" else echo "$1" fi } main(){ if [ $# -ne 1 ] then usage fi checkurl $1 } main $*
sh checkurl.sh www.vbill.cn
10、c语言文件下载,带进度条
https://blog.csdn.net/qq_31629063/article/details/80846673
https://www.cnblogs.com/wainiwann/p/3148884.html
url2file.c ,已经忘了在哪儿淘的。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <fcntl.h> #include <unistd.h> #include <netdb.h> #include <errno.h> #define HOST_NAME_LEN 256 #define URI_MAX_LEN 2048 #define RECV_BUF 8192 #define RCV_SND_TIMEOUT (10*1000) //收发数据超时时间(ms) typedef struct { int sock; //与服务器通信的socket FILE *in; //sock描述符转为文件指针,方便读写 char host_name[HOST_NAME_LEN]; //主机名 int port; //主机端口号 char uri[URI_MAX_LEN]; //资源路径 char buffer[RECV_BUF]; //读写缓冲 int status_code; //http状态码 int chunked_flag; //chunked传输的标志位 int len; //Content-length里的长度 char location[URI_MAX_LEN]; //重定向地址 char *save_path; //保存内容的路径指针 FILE *save_file; //保存内容的文件指针 int recv_data_len; //收到数据的总长度 time_t start_recv_time; //开始接受数据的时间 time_t end_recv_time; //结束接受数据的时间 } http_t; /* 打印宏 */ #define MSG_DEBUG 0x01 #define MSG_INFO 0x02 #define MSG_ERROR 0x04 static int print_level = /*MSG_DEBUG |*/ MSG_INFO | MSG_ERROR; #define lprintf(level, format, argv...) do{ if(level & print_level) printf("[%s][%s(%d)]:"format, #level, __FUNCTION__, __LINE__, ##argv); }while(0) #define MIN(x, y) ((x) > (y) ? (y) : (x)) #define HTTP_OK 200 #define HTTP_REDIRECT 302 #define HTTP_NOT_FOUND 404 /* 不区分大小写的strstr */ char *strncasestr(char *str, char *sub) { if(!str || !sub) return NULL; int len = strlen(sub); if (len == 0) { return NULL; } while (*str) { if (strncasecmp(str, sub, len) == 0) { return str; } ++str; } return NULL; } /* 解析URL, 成功返回0,失败返回-1 */ /* http://127.0.0.1:8080/testfile */ int parser_URL(char *url, http_t *info) { char *tmp = url, *start = NULL, *end = NULL; int len = 0; /* 跳过http:// */ if(strncasestr(tmp, "http://")) { tmp += strlen("http://"); } start = tmp; if(!(tmp = strchr(start, '/'))) { lprintf(MSG_ERROR, "url invaild "); return -1; } end = tmp; /*解析端口号和主机*/ info->port = 80; //先附默认值80 len = MIN(end - start, HOST_NAME_LEN - 1); strncpy(info->host_name, start, len); info->host_name[len] = '