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

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

问题描述:

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

I have bash script which takes 3 parameters from the command line. It compares all of the files in the directory to see if they are of the type of the first 2 parameters. If they are, the script converts such files to the type of the third parameter using an FFMPEG command. I would execute the script with the following command:

./convert.sh .avi .mp4 .flv 

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

That this, this script would convert all of the .avi and .mp4 files to .flv.

运行脚本时,出现错误

syntax error near unexpected token `do' in bash script.

这是代码:

#!/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

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

You have some issues with your formatting and syntax. sjsam's advice to use shellcheck is good, but the short version is that you should be using square brackets instead of round ones on the internal brackets of your if statement:

if [ ${file: -4} == "$1" ] || [ ${file: -4 } == "$2" ] {

而且我认为您不需要在ffmpeg行之前或在行的末尾使用大括号时才需要执行"do"操作,因此您最终会得到...

And I don't think you need the 'do' before your ffmpeg line or the curly bracket at the end of the line above, so you end up with...

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