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
That this, this script would convert all of the .avi and .mp4 files to .flv.
When I run the script, I get the error
syntax error near unexpected token `do' in bash script.
Here is the code:
#!/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
There are a bunch of syntax errors here. Let's start with the line:
You need a space between
if
and[
(or whatever comes after it). As written, the shell is treating "if[" as the name of a command, which isn't what you want at all.The
[ ... ]
style of conditional doesn't understand||
(it uses-o
instead), requires that all shell metacharacters (like parentheses) be escaped or quoted, might not understand==
(just=
is the standard), and will get very confused if any of the unquoted variable/parameter references are blank.if
conditionals end withthen
(either on the next line, or after a;
) not{
You could fix it like this:
Or, since you're using
bash
(instead of a more basic shell), you can use the[[ ... ]]
conditional style, which has much cleaner syntax:Next, remove the
do
beforeffmpeg
.do
is part of the syntax forfor
andwhile
loops; you already have one above (where it belongs), and this one just doesn't make sense. This is what's causing the error you see.Next, the way you're replacing the file's extension won't work right. The variable reference
"${file%.extension}"$3
will try to remove ".extension" (not the variable, just the string) from the end of$file
. It also has$3
outside the quotes, which can cause trouble. You could fix it by using"${file%$extension}$3"
instead, but frankly I'd just use"${file%.*}$3"
to remove the extension no matter what length it is (and I'd also redo theif
comparisons similarly, but that's more complicated).Finally, you need a
fi
(after theffmpeg
line) to end the conditional. Everyif
needs athen
and afi
.And just as a stylistic thing, you don't need
;
at the end of a line in shell; it's only needed when you're putting multiple commands (or things likedo
andthen
) on the same line. Anyway, here's my quick rewrite:For me, it was due to
CRLF
. It should beLF
for linux env.Your script could be reduced to simply:
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:
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...