简体   繁体   English

在bash脚本中转义字符时出现问题

[英]Problem escaping characters in bash script

I'm really new to bash script, I'm trying create a shell script to process videos using a cron but I really don't know what's going on 我真的是bash脚本的新手,我正在尝试创建一个shell脚本以使用cron处理视频,但我真的不知道发生了什么

#!/bin/bash

args=("$@")
count=0
startingfrom=5

args1="-r 29.97 -t 00:13:30 -vsync 0 -vpre libx264-medium -i"
args2='-vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] "'
args12="-r 29.97 -ss 00:40:30 -vsync 0 -vpre libx264-medium -i"
args3="-vcodec libx264 -acodec libfaac"

for file in /home/allen/s/
do


eppart1 = "$args[0]_$startingfrom_01_01_02.mp4"
eppart2 = "$args[0]_$startingfrom_01_02_02.mp4"

/usr/local/bin/ffmpeg $args1 "$file"  $args2 $args3 "${args[0]}_${startingfrom}_01_01_02.mp4"

mv "${args[0]}_${startingfrom}_01_01_02.mp4" upload/

/usr/local/bin/ffmpeg $args12 "$file" $args2 $args3 "${args[0]}_${startingfrom}_01_02_02.mp4"

mv "${args[0]}_${startingfrom}_01_02_02.mp4" upload/



let "count += 1"
let "startingfrom +=1"
echo "$count"

echo ${args[0]}
echo ${args[1]}

done

/usr/local/bin/python2.5 /home/allen/s/process.py

Problem starts in this argument 问题始于这个论点

args2='-vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] "'

I don't know if its because of the special character or not, usually if I type the whole command it works, but in bash script it's different 我不知道是否是因为特殊字符,通常如果我输入整个命令都可以,但是在bash脚本中是不同的

The output it gives me 它给我的输出

Unable to find a suitable output format for 'scale=580:380'

Though like I said, if I run the whole command it just works 尽管就像我说的那样,如果我运行整个命令,它就可以工作

prepend what will be executed with an echo, so you will see what will be passed to the commands; 在回显之前执行将要执行的操作,因此您将看到将传递给命令的内容;

           something $args2

will expand to 将扩展到

           something -vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] "

ie calling something with two arguments, one being an option (or 2 options) and the other is a "single" string; 例如,使用两个参数调用某个东西,一个是一个选项(或两个选项),另一个是“单个”字符串; I suppose you don't want the double quotes. 我想你不要双引号。

The line below has two problems. 下面的行有两个问题。 First, you cannot have spaces around the equals. 首先,等号周围不能有空格。 Second, when you try to use a variable within a double-quoted string, you need to use curly braces. 其次,当您尝试在双引号字符串中使用变量时,需要使用花括号。

eppart1 = "$args[0]_$startingfrom_01_01_02.mp4"

What you need is this: 您需要的是:

eppart1="$args[0]_${startingfrom}_01_01_02.mp4"

Otherwise, bash will be looking for a variable identified by startingfrom_01_01_02 , not startingfrom 否则,bash将寻找由startingfrom_01_01_02标识的变量,而不是startingfrom

You seem to have this fixed in later lines so maybe you just pasted an older version? 您似乎在以后的行中已解决此问题,所以也许您只是粘贴了旧版本?

Other than that, what the others said is correct: You should execute nothing but assignments and echos until you are sure your variables are correct. 除此之外,其他人所说的是正确的:除了确定赋值和正确值之前,您应仅执行赋值和回显操作。

Putting quotes inside a variable value (as you did with args2) does not do what you expect. 将引号放在变量值内(就像您对args2所做的那样)不会达到您的期望。 Specifically, they won't be parsed as quotes when the variable is used, but just as regular characters. 具体来说,使用变量时,它们不会被解析为引号,而会被解析为常规字符。 So if you do: 因此,如果您这样做:

args2='-vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] "'
somecmd $args2

it winds up being equivalent to: 它最终等于:

somecmd '-vf' '"[in]' 'scale=580:380' '[T1],[T1]' 'pad=720:530:0:50' '[out]' '"'

note that the double-quotes aren't being interpreted as grouping words together, instead they're just passed to the command as parts of the words -- not what you want at all. 请注意,双引号不会被解释为将单词分组在一起,而是将它们作为单词的一部分传递给命令-根本不是您想要的。 BTW, this is a case where debugging with echo won't help -- everything will look fine, because echo doesn't clearly separate the arguments it gets. 顺便说一句,在这种情况下,使用echo进行调试将无济于事-一切都会好起来的,因为echo并没有清楚地区分所获取的参数。 Instead, using set -x will get bash to print the commands as it executes them, with its parsing clarified. 取而代之的是,使用set -x将使bash在执行命令时打印命令,并澄清其解析。

Now, about fixing it. 现在,关于修复它。 If you need to store command arguments in a variable (as you're doing here) and keep track of where the word boundaries are (as you're trying to use double-quotes to do), the standard answer is to use arrays instead of simple variables. 如果您需要将命令参数存储在变量中(如此处所做的操作)并跟踪单词边界在哪里(如您尝试使用双引号一样),则标准答案是使用数组代替简单变量。 Create the array with each argument as an array element, then use the array with the idiom "${arrayname[@]}" (note that the double-quotes around it are very important). 创建每个参数作为数组元素的数组,然后将其与习惯用法"${arrayname[@]}"一起使用该数组(请注意,其周围的双引号非常重要)。 Here's my example above: 这是我上面的示例:

args2=(-vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] ")
somecmd "${args2[@]}"

or in your script: 或在您的脚本中:

...
args2=(-vf "[in] scale=580:380 [T1],[T1] pad=720:530:0:50 [out] ")

...

/usr/local/bin/ffmpeg $args1 "$file" "${args2[@]}" $args3 "${args[0]}_${startingfrom}_01_01_02.mp4"

mv "${args[0]}_${startingfrom}_01_01_02.mp4" upload/

/usr/local/bin/ffmpeg $args12 "$file" "${args2[@]}" $args3 "${args[0]}_${startingfrom}_01_02_02.mp4"
...

BTW, doing the same thing to your other "arg" variables isn't necessary (since they don't contain any arguments with spaces in them), but wouldn't hurt and might be considered good practice. 顺便说一句,不必对您的其他“ arg”变量执行相同的操作(因为它们不包含任何带有空格的参数),但不会受到损害,可以认为是一种好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM