为下载文件卷曲bash脚本 [英] Bash script for downloading files with Curl

查看:159
本文介绍了为下载文件卷曲bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拼凑下载文件的小批处理脚本,接受一个URL作为第一个参数和一个本地文件名作为其第二个参数。在测试过程中,我了解到,其在输出文件名空间绊倒了,所以我用sed来逃避他们尝试过,但它不工作。

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 I've learned that its tripping up on spaces in the output filename, so I've tried using sed to escape them, but its not working.

#!/bin/bash
clear
echo Downloading $1
echo
filename=`sed -e "s/ /\\\ /g" $2`
echo $filename
echo eval curl -# -C - -o $filename $1

但我得到的消息

sed的:outfile.txt:没有这样的文件或目录

sed: outfile.txt: No such file or directory

这表明其试图将输出文件作为输入加载到sed的,而不是治疗输出文件名作为一个字符串。

which suggests its trying to load the output file as input to sed instead of treating the output filename as a string literal.

你会在这里是正确的语法?

What would be the correct syntax here?

推荐答案

引用正确的参数,而不是改变他们可能是一个更好的办法

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

这是相当正常的期望有引用空格参数shell脚本

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

例如

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

称为像这样

./的MyScript http://www.foo.com我的文件

另外,逃脱的空间用'\\',你给他们打电话

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

./myscript http://www.example.com my\ other\ filename\ with\ spaces

这篇关于为下载文件卷曲bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆