如何将带有空格的绝对文件路径传递到linux shell脚本?

时间:2015-11-18 22:58:33

标签: linux shell command-line-interface whitespace handbrake

我想用linux shell脚本自动化我的视频转换。我在ubuntu 14.04上使用HandBrakeCLI和我的个人选项。但我陷入了某种困境。这是我想在伪代码中完成的事情:

# watch a specific folder (A)
- if a (source) file is older then 1d process it
- as soon as the process is finished move the source file into folder (B)
- the target for the new file is folder (C)
# process the files
1. find all the new files (older then 1d) within (A)
2. get the full path of the file and store it
3. replace the source folder path (A) with the target folder (C)
4. start conversion with HandBrakeCLI like:
   HandBrake $options $sourcefile $targetfile

我用此代码覆盖特定文件夹扫描的第一部分:

find $A -name "*.mkv" -ctime +1 -print0

我想将源文件的绝对路径从文件夹(A)传递到我的脚本'convertMkv'。棘手的部分就在这里:

find $A -name "*.mkv" -ctime +1 -print0 | xargs -0 -I {} ./convertMkv "{}" "$C" ;

我想将源文件从文件夹(A)和目标文件夹(C)传递到我的转换脚本,该脚本将准备必要的路径并触发HandBrakeCLI。

源文件的路径示例可以是:

  

“/ tmp / Interest.mkv的视频”

     

“/ tmp / File \ Folder / Lion_King.mkv”

     

“/ TMP / Avatar.mkv”

这是我的'convertMkv'脚本:

#!/bin/bash
source="$HOME/handbrake/.raw"
target="$2"

OPT=""
OPT="$OPT --verbose"
OPT="$OPT --encode x264"
OPT="$OPT --quality 20.0"
OPT="$OPT --format mp4"
...

# -----------------------------------------------------------------------------
# mkv2mp4()
# -----------------------------------------------------------------------------
function mkv2mp4
{
    input=$1
    output=$2

    HandBrakeCLI $OPT -i "$input" -o "$output" 2>&1 | tee "/tmp/log/Test.log"
}

function main
{
    path="${1%/*}"
    file="${1##*/}"
    newPath="${path##/*/}"

    mkv2mp4 $1 $target/$newPath/$file
}

main $@
exit 0

1 个答案:

答案 0 :(得分:2)

引用你的变量:

mkv2mp4 "$1" "$target/$newPath/$file"

main "$@"