Bash script for downloading files with Curl

后端 未结 4 1749
挽巷
挽巷 2021-01-13 15:47

I\'m trying to knock together a little batch script for downloading files, that takes a URL as its first parameter and a local filename as its second parameter. In testing

相关标签:
4条回答
  • 2021-01-13 16:06
    curl -# -C - -o "$2" $1
    
    0 讨论(0)
  • 2021-01-13 16:13

    quoting the arguments correctly, rather than transforming them might be a better approach

    It's quite normal to expect to have to quote spaces in arguments to shell scripts

    e.g.

    #!/bin/bash
    clear
    echo Downloading $1
    echo `curl -# -C - -o "${2}" "${1}"`
    

    called like so

    ./myscript http://www.foo.com "my file"

    alternatively, escape the spaces with a '\' as you call them

    ./myscript http://www.example.com my\ other\ filename\ with\ spaces
    
    0 讨论(0)
  • 2021-01-13 16:23

    if $2 is a text input then try

    echo $2 | sed 's: :\\\ :g '
    

    I generally avoid backslashes in sed delimiters are they are quite confusing.

    0 讨论(0)
  • 2021-01-13 16:29

    I agree with cms. Quoting the input arguments correctly is much better style - what will you do with the next problem character? The following is much better.

    curl -# -C - -o "$2" $1
    

    However, I hate people not answering the asked question, so here's an answer :-)

    #!/bin/bash
    clear
    echo Downloading $1
    echo
    filename=`echo $2 | sed -e "s/ /\\\ /g"`
    echo $filename
    echo eval curl -# -C - -o $filename $1
    
    0 讨论(0)
提交回复
热议问题