专业人士可以做Bash + Escape

时间:2011-04-10 14:18:45

标签: linux bash unix escaping

我花了整整一夜试图完成过程,但我的所有尝试都以失败告终。

我写了一个非常简单的脚本来清除我想要做的事情,请复制它并尝试启动它。

    #!/bin/bash
set -x
urls='http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'
#urls="http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3"


for letsgo in `curl -OLJg  "'${urls}'"` ; do
echo "GOT TRIED OF TRYING"
done

# for letsgo in `curl -OLJg $urls` ; do
#echo "GOT TRIED OF TRYING"
# done

启动后得到的结果

First Loop Way: -

./ap2.sh
+ urls='http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'
++ curl -OLJg ''\''http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'\'''

curl: (1) Protocol 'http not supported or disabled in libcurl
+ for letsgo in '`curl -OLJg  "'\''${urls}'\''"`'
+ echo 'GOT TRIED OF TRYING'
GOT TRIED OF TRYING

第二圈

./ap2.sh
+ urls='http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'
++ curl -OLJg http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine '(Original' 'Mix).mp3'
curl: option -: is unknown
curl: try 'curl --help' or 'curl --manual' for more information

问题是我不知道在未经我许可的情况下从哪里撤回网址并且可能无法正常工作。

更新

我通过使用

摆脱它
for letsgo in `curl -OLJg  "${urls}"` ; do
echo "Working Fine But We Still Have Problem When We Are Using More Than 1 URL"
done

当脚本有多个url时,问题必须在引号中(仅限我的情况)才能使curl工作。我可以在linux控制台中手动完成,没有任何问题但是当使用BASH脚本时,这些脚本的结果将是

#!/bin/bash
set -x
urls="'http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3' -OLJg 'http://webprod15.megashares.com/index.php?d01=3109985&lccdl=9e8e091ef33dd103&d01go=1' -OLJg "



for letsgo in `curl -OLJg "${urls}"` ; do
    echo "Working Fine But We Still Have Problem When We Are Using More Than 1 URL"
done

结果: -

+ urls=''\''http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'\'' -OLJg '\''http://webprod15.megashares.com/index.php?d01=3109985&lccdl=9e8e091ef33dd103&d01go=1'\'' '
++ curl -OLJg ''\''http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3'\'' -OLJg '\''http://webprod15.megashares.com/index.php?d01=3109985&lccdl=9e8e091ef33dd103&d01go=1'\'' '

curl: (1) Protocol 'http not supported or disabled in libcurl
+ for letsgo in '`curl -OLJg "${urls}"`'
+ echo 'Working Fine But We Still Have Problem When We Are Using More Than 1 URL'
Working Fine But We Still Have Problem When We Are Using More Than 1 URL

我只是想让它正常工作,就像我在linux控制台上输入它一样,没有bash通过转义字符串所造成的中断。像这样

curl -OLJg 'http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3' -OLJg 'http://webprod15.megashares.com/index.php?d01=3109985&lccdl=9e8e091ef33dd103&d01go=1' -OLJ

6 个答案:

答案 0 :(得分:2)

你应该删除curl参数中的单引号 - 你写它的方式它们会成为URL的一部分。

答案 1 :(得分:1)

你有没有在浏览器地址栏中看到空白?它们必须转换为%A20 (由Lucas的评论更正,谢谢)%20,也许还有类似的特殊字符。

wellurl=$(echo $urls | sed 's/ /%20/g')

我不知道卷曲 - 它类似于wget,不是吗?

wget -np $wellurl
2011-04-10 16:55:28 (17,2 MB/s) - »An-Beat - Mentally Insine (Original Mix).mp3« gespeichert [191]

为我工作。

更新

要从脚本中获取多个URL,请使用数组:

#!/bin/bash
#
declare -a urls
urls=('http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3' 'http://webprod15.megashares.com/index.php?d01=3109985&lccdl=9e8e091ef33dd103&d01go=1')

for i in $(seq 0 ${#urls[@]} )
do 
    wellurl=$(echo ${urls[i]} | sed 's/ /%20/g')
    # echo "$wellurl"
    curl -OLJg "$wellurl"
done
  • $ {#urls [@]}返回数组中元素的数量
  • 不要将选项放入数组
  • 请在下一个问题中使用较小的网址,请:)

答案 2 :(得分:1)

答案 3 :(得分:0)

试试这个

urls="http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3"
curl -OLJg  "${urls}" | while read results
do
 ...
done

答案 4 :(得分:0)

使用名为urls的变量表明其中将有多个URL。如果是这样,您可以考虑使用BASH数组。此外,回声消息“GOT TIRED OF WAITING”表明卷曲可能会失败。如果是这样,您可以考虑更明确地检查错误。

检查并尝试运行以下内容:

set -x
list_of_urls=('http://bellatrix.oron.com/jrmxp36wf36yew4veg4fp53kmwlogjeecmopy3n2ja5yqkyzekxwqx2pckq6dtd5hb7duvgk/An-Beat - Mentally Insine (Original Mix).mp3')

for url in "${list_of_urls[@]}"
do
 curl -s -OLJg  "${url}"
 if [ $? -gt 0 ]; then
   echo "$url is a PROBLEM! (return code: $?)" 
 fi
done

运行此操作时我注意到的一件事:服务器“成功”(换句话说,$?等于0),但curl返回一个名为error.html的文件。这可能是您陷入的另一个错误情况。祝你好运!

答案 5 :(得分:0)

卷曲-K对你有帮助吗? (您可以将文件中的网址完全按照您在导航栏中的显示方式) http://curl.haxx.se/docs/manpage.html