bash 脚本中意外标记“do”附近的语法错误

Avi*_*Avi 6 unix bash ffmpeg

我有 bash 脚本,它从命令行获取 3 个参数。它比较目录中的所有文件,看它们是否属于前 2 个参数的类型。如果是,脚本将使用 FFMPEG 命令将此类文件转换为第三个参数的类型。我将使用以下命令执行脚本:

./convert.sh .avi .mp4 .flv 
Run Code Online (Sandbox Code Playgroud)

这样,此脚本会将所有 .avi 和 .mp4 文件转换为 .flv。

当我运行脚本时,出现错误

syntax error near unexpected token `do' in bash script.
Run Code Online (Sandbox Code Playgroud)

这是代码:

#!/bin/bash

# $1 is the first parameter passed
# $2 is the second parameter passed 
# $3 is the third parameter passed

for file in *.*; 
    do 
        #comparing the file types in the directory to the first 2 parameters passed
        if[  ( ${file: -4} == "$1" ) || ( ${file: -4 } == "$2" )  ]{
            export extension=${file: -4}
            #converting such files to the type of the first parameter using the FFMPEG comand
            do ffmpeg -i "$file" "${file%.extension}"$3;
done
Run Code Online (Sandbox Code Playgroud)

She*_*hid 7

对我来说,这是由于CRLF. 它应该LF适用于 linux env。


Gre*_*reg 0

您的格式和语法存在一些问题。sjsam 关于使用 shellcheck 的建议很好,但简短的版本是您应该在 if 语句的内部括号中使用方括号而不是圆括号:

if [ ${file: -4} == "$1" ] || [ ${file: -4 } == "$2" ] {
Run Code Online (Sandbox Code Playgroud)

我认为你不需要 ffmpeg 行之前的“do”或上面行末尾的大括号,所以你最终会得到......

for file in *.*; 
    do 
    #comparing the file types in the directory to the first 2 parameters passed
    if [  ${file: -4} == "$1" ] || [ ${file: -4 } == "$2" ]
        export extension=${file: -4}
        #converting such files to the type of the first parameter using the FFMPEG comand
        ffmpeg -i "$file" "${file%.extension}"$3;
    fi
done
Run Code Online (Sandbox Code Playgroud)