小编Lef*_*ium的帖子

将带有空格和引号的参数传递给脚本(不引用所有内容)

以下内容在命令行上效果很好:

$ ffmpeg -i input.m4a -metadata 'title=Spaces and $pecial char'\''s' output.m4a
Run Code Online (Sandbox Code Playgroud)

如何参数化此命令并在脚本/函数中使用它?我想添加多个元数据标签,如下所示:

$ set-tags.sh -metadata 'tag1=a b c' -metadata 'tag2=1 2 3'
Run Code Online (Sandbox Code Playgroud)

更新:

我把我的问题简化得有点太多了。我实际上想调用一个脚本,该脚本使用其中的参数化命令来调用脚本。

这是我的确切用例:

此函数将文件转换为有声读物格式(在 .profile 中定义):

# snippet of .profile
convert_to_m4b () {
    FILE="$1"
    BASENAME=${FILE%.*}; shift

    ffmpeg -i "$FILE" -vn -ac 1 -ar 22050 -b:a 32k "$@" tmp.m4a &&
    mv tmp.m4a "$BASENAME.m4b"
}; export -f convert_to_m4b
Run Code Online (Sandbox Code Playgroud)

函数convert_to_m4b是从download-and-convert.sh中调用的:

#/bin/sh
 MP3_URL=$1; shift
FILENAME=$1; shift

if [ ! -f "${FILENAME}.mp3" ]; then
    curl --location --output "${FILENAME}.mp3" "$MP3_URL"
fi

convert_to_m4b "${FILENAME}.mp3" "$@" …
Run Code Online (Sandbox Code Playgroud)

shell-script quoting function whitespace arguments

3
推荐指数
1
解决办法
6232
查看次数

标签 统计

arguments ×1

function ×1

quoting ×1

shell-script ×1

whitespace ×1