【问题标题】:Bash curl: Saving a file with a partial name of the URLBash curl:使用 URL 的部分名称保存文件
【发布时间】:2018-12-16 15:33:48
【问题描述】:

我尝试使用 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"}

有什么想法吗?

【问题讨论】:

    标签: bash curl


    【解决方案1】:

    相信你在找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
    

    【讨论】:

    • 是的,它很好,但我需要以某种方式将文件名插入到变量中(在循环内)。我可以这样做吗?
    • @bugnet17 你能编辑你的问题以显示/var/script/string.txt 的最后一行是什么吗?没有它,我无法真正重现您的脚本的输出。
    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2022-11-17
    • 2023-03-18
    • 1970-01-01
    • 2020-03-27
    • 2011-12-03
    相关资源
    最近更新 更多