在论证中逃避冒号,等于,&符号

时间:2012-03-29 20:14:42

标签: bash command-line escaping quotes

下面是我的代码,它表现得非常离奇,我不确定我理解原因,但我认为它与路径扩展有某种联系:

编辑:这是第二个清单中提到的asxgrab脚本:

#!/usr/bin/env bash

let "i=0"

GET "$1" | grep 'href="mms://' | while read line
do
    echo "$line"
    address=`echo $line | grep -Eow '"[^"]+"'`
    address_length=$((${#address}-2))
    address=${address:1:address_length}
    echo "Grabbing '$address', attempt $i, $address_length"
    mplayer "'$address' -dumpstream -dumpfile '$2.wmv' -vf scale\=320\:240" > /dev/null
    if [ ! $? ]; then
        break 1
    fi
    let "i++"
done

我已经尝试过几十种不同的逃避,引用和诸如此类的方法,我想我已经耗尽了我的创造力......请帮忙!

假设使用以下参数调用脚本:

./asxgrab 'http://switch3.castup.net/cunet/gm.asp?ai=16&ip=79.180.37.230&course=c20478/2012b&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&ar=c20478_12b_784_01_1&ticket=58353E7A69BF1B9XO655E9E683016C5255C08DD37211ECF495F72A64E5278AA434CSP3C10C028D604AAD6C39FE7D71386D230983BBAF58A2511D14222F95090AE2C4C55CB696B82919A5A8EB44EAF8954DEDB5' 'java.0'

收到的回复如下:

<asx version="3.0">
    <!-- GMX -->
    <param name="encoding" value="utf-8" />
    <title>CastUP: c20478_12b_784_01_1</title>
    <MOREINFO HREF = "" />

    <entry>
    <PARAM NAME="EntryType" VALUE="Content" />

    <param name="encoding" value="utf-8" />
    <PARAM NAME="CastUP_AssociatedURL" VALUE="" />
    <PARAM NAME="CastUP_Content_Config" VALUE="" />
    <PARAM NAME="CastUP_Content_ClipMediaID" VALUE="9067325" />
    <author>openu</author>
        <title>CastUP: c20478_12b_784_01_1</title>
<ref href="mms://s3fwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=FE81687B-33CD-4441-A8F6-1DEC9923332C&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183" />
<ref href="...." />

    </entry>

当我这样做时:

mplayer "mms://s3fwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=C6527A33-587F-4148-A3D2-E7EDED51316B&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183" -dumpstream -dumpfile java0.wmv -vf scale=320:240
从终端

它可以工作,但它不能从脚本中工作。相反,它试图修剪$line或类似的东西。我甚至不明白会发生什么,但输出绝对没有后果......

这是我的uni网站,这很有趣,有关Java的讲座:)

修改

<ref href="mms://s0dwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=111A3770-29DC-40EC-85EC-93453E2819D7&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183" />
Grabbing 'mms://s0dwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=111A3770-29DC-40EC-85EC-93453E2819D7&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183', attempt 0, 268
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
No stream found to handle url 'mms://s0dwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=111A3770-29DC-40EC-85EC-93453E2819D7&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183' -dumpstream -dumpfile 'java.0.wmv' -vf scale=320:240
d324d1043027b0720c295b93e4abb183" />
Grabbing '', attempt 1, -2
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
File not found: ''' -dumpstream -dumpfile 'java.0.wmv' -vf scale=320:240'
Failed to open '' -dumpstream -dumpfile 'java.0.wmv' -vf scale=320:240.
027b0720c295b93e4abb183" />

这是我从脚本中获得的输出。

MPlayer SVN-r31918 (C) 2000-2010 MPlayer Team
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing mms://s3fwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=C6527A33-587F-4148-A3D2-E7EDED51316B&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183.
STREAM_ASF, URL: mms://s3fwm.castup.net/server12/16/798/79837168-61.wmv?ct=IL&rg=BZ&aid=16&ts=0&cu=C6527A33-587F-4148-A3D2-E7EDED51316B&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183
Resolving s3fwm.castup.net for AF_INET6...

Couldn't resolve name for AF_INET6: s3fwm.castup.net
Resolving s3fwm.castup.net for AF_INET...
Connecting to server s3fwm.castup.net[212.179.13.65]: 1755...

Connected
file object, packet length = 6100 (6100)
unknown object
unknown object
unknown object
stream object, stream ID: 1
stream object, stream ID: 2
unknown object
data object
mmst packet_length = 6100
Cache size set to 64 KBytes
Stream not seekable!

这是预期的产出。

3 个答案:

答案 0 :(得分:2)

如果我正确地理解了一切,我看到的唯一真正的问题是这一行:

    mplayer "'$address' -dumpstream -dumpfile '$2.wmv' -vf scale\=320\:240" > /dev/null

应该是这样的:

    mplayer "$address" -dumpstream -dumpfile "$2.wmv" -vf scale=320:240 > /dev/null

也就是说,您需要删除将所有mplayer的参数收集到一个参数中的双引号,并且需要删除其中的单引号和反斜杠,其中一些参数

为清洁起见,我可能会改变其他事情:

  • read line更改为read -r line可以消除反斜杠带来的任何奇怪现象。
  • line="${line%$'\r'}" - 循环体开头的一条线while可以解决任何关于杂散回车问题的担忧。
  • address=`echo $line ...更改为address=`echo "$line" ...可以消除任何分词和文件扩展的风险等等(并非它们看起来非常可能)。
  • [ ! $? ]测试没有意义(因为$?永远不会是空白,是吗?);在上一个命令结束时将它改为&& break 1将更加清晰恕我直言,更有效。

答案 1 :(得分:2)

你试过这个吗?

mplayer "$address" -dumpstream -dumpfile "$2.wmv" -vf "scale=320:240" > /dev/null

bash的秘密:

  • 双引号内的变量被扩展;那些单引号内的人不是。
  • 引号内的单词被视为单个标记。
  • 一种类型的行情逃脱了另一种类型的行情。

原始行

mplayer "'$address' -dumpstream -dumpfile '$2.wmv' -vf scale\=320\:240" > /dev/null

使用单个参数mplayer调用'$address' -dumpstream -dumpfile '$2.wmv' -vf scale\=320\:240,但mplayer希望每个令牌都是一个单独的单词。

答案 2 :(得分:1)

你的bash脚本非常“势在必行”,因为它已经从Assembly重写。

另一种方法:

  • 获取描述符文件
  • 清理描述符文件(在其中留下mms:// ...字符串)
  • 使用该清理结果作为循环处理的输入

这是一个班轮,我只是分手了。你将代码放在do..done之间。我不关心文件名是巨大的,我把它们作为输出.wmv:

link="http://switch3.castup.net/cunet/gm.asp?ai=16&ip=79.180.37.230&course=c20478/2012b&cuud=user:77f6c69526b9fa05f78d3d6679630308d324d1043027b0720c295b93e4abb183&ar=c20478_12b_784_01_1&ticket=58353E7A69BF1B9XO655E9E683016C5255C08DD37211ECF495F72A64E5278AA434CSP3C10C028D604AAD6C39FE7D71386D230983BBAF58A2511D14222F95090AE2C4C55CB696B82919A5A8EB44EAF8954DEDB5"

for i in $(curl -s "$link" | awk '/mms/ {gsub (/<ref href="/, ""); gsub(/" \/>/, ""); print $0}')
do
  mplayer $i -dumpstream -dumpfile $i.wmv -vf scale=320:240
done