简体   繁体   English

将值解析为Shell Bash脚本

[英]Parsing Values To Shell Bash Script

I am trying to make a simple bash script for a command so i can make it reusable for me the command has been taken from another post to convert videos with ffmpeg then upload. 我正在尝试为命令创建一个简单的bash脚本,以便我可以重用该命令,该命令是从另一篇帖子中摘录来用ffmpeg转换视频然后上传的。

Here is what i have so far. 这是我到目前为止所拥有的。

#!/bin/bash
MOVIES=$1
EXT=$2
BUCKET=$3
find "$MOVIES" -name "*.$EXT" -exec sh -c 'ffmpeg -i "$0" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "${0%%.mov}.mp4" && s3cmd put "${0%%.mov}.mp4" "$BUCKET""$(basename "${0%%.mov}.mp4")"' {} \;
exit;

I am having issues with the third var if i run it like this. 如果我像这样运行它,我就遇到了第三个变量的问题。

sh batch.sh ~/Documents/screengrabs/ mov s3://bucket/

I am getting a error it encodes properly. 我收到一个错误,它编码正确。

ERROR: Parameter problem: Destination must be S3Uri. Got: file://49A22352-9F41-48B9-BF97-610CBF699025-630-0000055828D0D55F.mp4

This means it is not parsing the $3 parameter $BUCKET properly. 这意味着它没有正确解析$ 3参数$ BUCKET。

Any help this is my first bash script attempt. 任何帮助,这是我第一次尝试bash脚本。

Thanks 谢谢

Update still not working 更新仍然不起作用

#!/bin/bash
MOVIES="$1"
EXT="$2"
BUCKET="$3"

find "$MOVIES" -name "*.${EXT}" -exec sh -c 'ffmpeg -i "$0" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "${0%%.mov}.mp4" && s3cmd put "${0%%.mov}.mp4" "${BUCKET}/sam.mp4"' {} \;

exit;

WORKING 工作

#!/bin/bash
MOVIES=$1
EXT=$2
export BUCKET=$3
find "$MOVIES" -name "*.$EXT" -exec sh -c 'ffmpeg -i "$0" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "${0%%.mov}.mp4" && s3cmd put "${0%%.mov}.mp4" "$BUCKET""$(basename "${0%%.mov}.mp4")"' {} \;
exit;

The problem is that $0 is the name of the script that is executed (in this case a temporary file created by sh from the argument to -c ), not the first argument to the resulting script. 问题在于$0是执行脚本的名称(在本例中是sh-c的参数创建的临时文件),而不是结果脚本的第一个参数。 Note that the error message does not mention a file with sam in the name, so it is being generated by the ${0%%.mov}.mp4 argument to s3cmd , not the one that uses $BUCKET . 请注意,错误消息没有提到与文件sam的名字,所以它是由生成的${0%%.mov}.mp4参数s3cmd ,而不是使用一个$BUCKET Replace it with $1 . 将其替换为$1 Also, you need to export BUCKET so that the shell started by find has access to it. 另外,您需要导出BUCKET以便以find开头的shell find访问它。 (The literal string $BUCKET , not its value, is passed to sh via its -c option because the string is rightly single-quoted.) (文字字符串$BUCKET而非其值通过其-c选项传递给sh ,因为该字符串正确地被单引号引起了。)

(UPDATE: as Etan pointed out, $0 is correct.) (更新:正如Etan所指出的, $0是正确的。)

#!/bin/bash
MOVIES=$1
EXT=$2
export BUCKET=$3
find "$MOVIES" -name "*.$EXT" -exec sh -c 'ffmpeg -i "$0" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "${1%%.mov}.mp4" && s3cmd put "${0%%.mov}.mp4" "$BUCKET""$(basename "${0%%.mov}.mp4")"' {} \;
exit;

In bash 4, however, I would just write 但是,在bash 4中,我只写

#!/bin/bash
MOVIES=$1
EXT=$2
BUCKET=$3

shopt -s globstar
for movie in "$MOVIES"/**/*."$EXT"; do
    mp4movie=${movie%%.mov}.mp4
    ffmpeg -i "$movie" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "$mp4movie" &&
      s3cmd put "$mp4movie" "$BUCKET/$mp4movie"
done

This is much easier to get right than trying to cram a small script into a single string for sh -c . 与尝试将小脚本塞入sh -c的单个字符串中相比,这样做容易得多。


Manual recursive search with bash 3.2: 使用bash 3.2进行手动递归搜索:

process_file () {
    movie=$1
    mp4movie=${movie%%.mov}.mp4
    ffmpeg -i "$movie" -c:v libx264 -crf 22 -c:a libfaac -movflags faststart "$mp4movie" &&
      s3cmd put "$mp4movie" "$BUCKET/$mp4movie"
}

process_directory () {
    dir=$1
    for name in "$dir"/*; do
        if [[ -d "$name" ]]; then
            process_directory "$name"
        elif [[ -f $name && $name = *.$EXT ]]; then 
            process_file "$name"
        fi
    done
}

MOVIES=$1
EXT=$2
BUCKET=$3
process_directory "$MOVIES"

@chepner's answer about using a "native" shell script is a good one. @chepner关于使用“本机” shell脚本的答案是一个很好的答案。 That being said, and as I just posted in a comment on the OP, I believe the issue here is simply a variable scope issue. 话虽如此,正如我刚刚在OP上发表的评论中一样,我认为这里的问题只是可变范围问题。

If you edit the script to use "$1" instead of "$BUCKET" in the script passed to sh -c and add "$BUCKET" between the {} and \\; 如果在传递给sh -c的脚本中编辑脚本以使用"$1"代替"$BUCKET" ,并在{}\\;之间添加"$BUCKET" \\; I believe the script will work (though I haven't fully evaluated the script in other respects). 我相信该脚本可以正常工作(尽管我还没有在其他方面对脚本进行全面评估)。

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

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