0

这是一个脚本.. 只是想练习一些 bash 技能并为我的中文 mp4 播放器制作一个快速实用程序 =)

#!/bin/bash

#####################################
# RockChip 4gb Player mencoder preset
#####################################

TOOL='mencoder'
OUTS='./out/'
OPT1='-noodml'
OPT2="-of avi -ofps 22 -vf-add scale=320:-2,expand=320:240 -srate 44100 -ovc xvid -xvidencopts bitrate=400:max_bframes=0:quant_type=s16le -oac lavc -lavcopts acodec=mp2:abitrate=128"

bold=`tput bold`
normal=`tput sgr0`

# check does argument exists
if test -z "$1"; then
  echo "There's no file given =)"
fi

# Check is it dir or file

if [ -d $1 ]; then
  echo "Directory is given: $1"

  # Test if output argument is given
  if [ -z $2 ]; then
        echo "No output argument given using default: ${bold}${red}$OUTS${normal}"
        mkdir out
  else
      # test is given path a directory
        if [ -d $2 ]; then
                OUT="$2"
        else
           echo "Output argument is not a directory"
        fi
   fi

OLD_IFS=IFS; IFS=$'\n'

for file in `find . -name "*.*" -type f | sed 's!.*/!!'` ; do
        file=`printf "%q" "$file"`
echo    ${TOOL} ${OPT1} ${file} -o ${OUTS}${file} ${OPT2}
done

IFS=OLD_IFS

fi

问题是这一行:

echo    ${TOOL} ${OPT1} ${file} -o ${OUTS}${file} ${OPT2}

当您删除 echo 以执行命令时,命令会失败,但是如果您要复制此回显脚本并手动执行它,则一切正常。

从 shell 脚本输出执行命令时:

MEncoder 1.0rc4-4.2.1 (C) 2000-2010 MPlayer Team
158 audio & 340 video codecs
-of avi -ofps 22 -vf-add scale=320:-2,expand=320:240 -srate 44100 -ovc xvid -xvidencopts bitrate=400:max_bframes=0:quant_type=s16le -oac lavc -lavcopts acodec=mp2:abitrate=128 is not an MEncoder option

Exiting... (error parsing command line)

正如我在手动执行命令之前提到的,一切正常,例如:

mencoder -noodml 12\ I\ Love\ You\ 1\ \ I\ Love\ You\ 2\ \ I\ Love\ You\ 3.avi -o ./out/12\ I\ Love\ You\ 1\ \ I\ Love\ You\ 2\ \ I\ Love\ You\ 3.avi -of avi -ofps 22 -vf-add scale=320:-2,expand=320:240 -srate 44100 -ovc xvid -xvidencopts bitrate=400:max_bframes=0:quant_type=s16le -oac lavc -lavcopts acodec=mp2:abitrate=128

现在我所能做的就是复制粘贴生成的命令..问题出在哪里?我真的很努力地用谷歌搜索......没有结果......(我知道mencoder有个人资料......这不是我想要的情况)

4

5 回答 5

1

你有(我相信第 37 行):

OUT="$2"

但我认为你的意思是:

OUTS="$2"
于 2011-04-21T04:35:46.967 回答
1

我不完全确定,但也许最好用双引号 ( ") 引用文件名而不是用printf "%q" "$file".

所以替换:

file=`printf "%q" "$file"`
${TOOL} ${OPT1} ${file} -o ${OUTS}${file} ${OPT2}

${TOOL} ${OPT1} "${file}" -o "${OUTS}${file}" ${OPT2}
于 2011-04-21T04:45:44.220 回答
1

首先,使用$()而不是反引号。

bold=$(tput bold)
normal=$(tput sgr0)

OLD_IFS=IFS; IFS=$'\n'应该是OLD_IFS=$IFS。你想得到 IFS 的值,所以放一个美元符号。

您无需调用sed即可获取文件的基本名称

while read -r  file
do
    filename="${file##*/}"
    filename=$(printf "%q" $filename)
    echo mencoder "${OPT1}" "${file}" -o "${OUTS}${file}" "${OPT2}"
done < <(find . -name "*.*" -type f)

最后,

IFS=OLD_IFS应该IFS=$OLD_IFS

于 2011-04-21T05:55:55.143 回答
0

您在for循环之前有此语句:

IFS=$'\n'

这会将您的内部字段分隔符设置为换行符,而不是默认匹配任何空格。这改变了替换参数的解析方式。在构造命令的行中:

${TOOL} ${OPT1} ${file} -o ${OUTS}${file} ${OPT2}

将会发生的是,每个${variable}表达式都将被扩展,然后 shell 将尝试将它们拆分为\n,而不是您通常期望的空白。它将为您提供与以下类似的结果(除非这些变量之一包含换行符):

"${TOOL}" "${OPT1}" "${file}" -o "${OUTS}${file}" "${OPT2}"

在这里,您可以看到您将整个${OPT2}字符串作为单个参数传递,而不是允许 Bash 将其拆分为空格并单独传递每个标志。mencoder然后被这个不知道如何处理的巨大参数弄糊涂了。当然,由于空格都还在,它们会被命令打印出来,并且在没有重置echo的shell中可以正常工作。$IFS

您可以通过定义一个简单的函数来很容易地演示这种效果,该函数将在单独的行上打印其每个参数:

$ print_args() { for arg; do echo $arg; done }
$ foo="1 2"
$ print_args ${foo} ${foo}
1
2
1
2
$ IFS=$'\n'
$ print_args ${foo} ${foo}
1 2
1 2

我建议不要在你的循环中使用这个$IFS技巧。for相反,您可以使用while read file迭代输入中的每一行。我还建议不要使用printf "%q"转义空格,而只需将参数引用到mencoder,这会将整个内容作为单个参数传递。请注意,我引用${file}${OUTS}${file}确保将它们作为单个参数传递给每个参数,而不是引用${OPT1}${OPT2}允许它们被 shell 解析为单独的参数。

find . -name "*.*" -type f | sed 's!.*/!!' | while read -r file
do 
    "${TOOL}" ${OPT1} "${file}" -o "${OUTS}${file}" ${OPT2}
done

顺便说一句,我建议您使用$()for 命令替换而不是``; 它之所以更受欢迎有很多原因,例如可读性、在其中更合理的引用和转义规则,以及嵌套多级命令替换的能力。乔纳森和韦斯指出的问题值得一提,尽管它们并不是导致您眼前问题的原因。

于 2011-04-21T04:54:04.850 回答
0

使用... | xargs bash -c '...'您只需要转义文件名中嵌入的单引号。

TOOL='mencoder'
OUTS='./out/'
OPT1='-noodml'
OPT2='-of avi -ofps 22 -vf-add scale=320:-2,expand=320:240 -srate 44100 -ovc xvid -xvidencopts bitrate=400:max_bframes=0:quant_type=s16le -oac lavc -lavcopts acodec=mp2:abitrate=128'

# test cases
file='12 I Love You 1  I Love You 2  I Love You 3.avi'   
file='12 I Lo"ve You 1  I Love You 2  I Love You 3.avi'   # one embedded double quote
file='12 I Lo"ve You 1  I Lo"ve You 2  I Love You 3.avi'  # two embedded double quotes
file="12 I Lo've You 1  I Love You 2  I Love You 3.avi"   # one embedded single quote
file="12 I Lo've You 1  I Lo've You 2  I Love You 3.avi"  # two embedded single quotes

# escape embedded single quotes in $file
escsquote="'\''"
file="${file//\'/${escsquote}}"

# we're passing ${TOOL} as arg0 to bash -c (which then is available as "$0")
# put the variables in the printf line into single quotes to preserve spaces
# remove no-op ':' to execute the command

printf '%s\n' "${file}"

printf '%s' "${OPT1} '${file}' -o '${OUTS}${file}' ${OPT2}" | 
    xargs bash -c 'set -xv; printf "%s\n" "$0" "$@" | nl' "${TOOL}"

printf '%s' "${OPT1} '${file}' -o '${OUTS}${file}' ${OPT2}" | 
    xargs bash -c 'set -xv; : "$0" "$@"' "${TOOL}"
于 2011-04-21T09:24:43.727 回答