bash - Bash curl:使用 URL 的部分名称保存文件
问题描述
我尝试使用 URL 的名称保存我下载的文件,但由于 Linux 中的文件夹结构,这是不可能的。相反,我想用最后一个斜杠之后的名称保存文件。此外,我需要以某种方式将文件名插入到变量中(在循环内)。
url=$(tail -1 /var/script/string.txt | grep -oP '(?<=expanded_url":")[^"]+' | sed 's/\\//g;s@^@@' | sed 's|/$||')
for link in $url; do
curl -k $url -o ./$link.html
done
错误输出:
https://pastebin.com/11pn54uH
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Failed to create the file ./https://pastebin.com/11pn54uH.html: No
Warning: such file or directory
100 1019 0 1019 0 0 2358 0 --:--:-- --:--:-- --:--:-- 2353
curl: (23) Failed writing body (0 != 1019)
所以,下载的文件名应该是这样的:
11pn54uH.html
字符串.txt
{"created_at":"Mon Dec 17 08:15:55 +0000 2018","id":1074578927270678528,"id_str":"1074578927270678528","text":"https:\/\/t.co\/ISaiyHDHBy fdg fds g\n#osectraining","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":961508561217052675,"id_str":"961508561217052675","name":"Online Security","screen_name":"osectraining","location":"Israel","url":"https:\/\/www.onlinesecurity.co.il","description":"OnlineSecurity provides online cyber-security training courses and certification, from beginner to advanced with the most advanced virtual labs in the field.","translator_type":"none","protected":false,"verified":false,"followers_count":2,"friends_count":51,"listed_count":0,"favourites_count":0,"statuses_count":19,"created_at":"Thu Feb 08 07:54:39 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1B95E0","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/961510231346958336\/d_KhBeTD_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/961510231346958336\/d_KhBeTD_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/961508561217052675\/1518076913","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"osectraining","indices":[34,47]}],"urls":[{"url":"https:\/\/t.co\/ISaiyHDHBy","expanded_url":"https:\/\/pastebin.com\/11pn54uH","display_url":"pastebin.com\/11pn54uH","indices":[0,23]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"fr","timestamp_ms":"1545034555629"}
有任何想法吗?
解决方案
我相信您正在寻找curl -O
:
-O, --remote-name Write output to a file named as the remote file
结果:
$ curl -O https://pastebin.com/11pn54uH.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14336 0 14336 0 0 159k 0 --:--:-- --:--:-- --:--:-- 160k
$ ls
11pn54uH.html
推荐阅读
- .net - 如何发现程序集中存在哪些 XAML 资源?
- android - api字符串响应的AES加密在开始时返回额外的字符
- python - 在 Python 中绘制 3D 条件函数
- angular - 在 anuglar 组件 html 文件中出现错误:找不到带有路径的控件
- salesforce - APEX:从内部 pbx 集成到 Salesforce
- android - 使用基于广告服务 UUID 的扫描过滤器扫描 BLE 外围设备
- docusignapi - 在用户签署文件后,我在哪里可以检查 docusign 是否调用了我的重定向 URL?我在哪里可以检查网址是否正确?
- php - PHP json解码数组反向
- docusignapi - 用于将通知/提醒发送到进程内信封的 docusign REST API
- c# - 设置父级时是否可以初始化嵌套属性?