syntax error near unexpected token `do' in bash script
Solution 1
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" ] {
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
Solution 2
For me, it was due to CRLF
. It should be LF
for linux env.
Solution 3
There are a bunch of syntax errors here. Let's start with the line:
if[ ( ${file: -4} == "$1" ) || ( ${file: -4 } == "$2" ) ]{
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:
if [ \( "${file: -4}" = "$1" \) -o \( "${file: -4}" = "$2" \) ]; then
Or, since you're using bash
(instead of a more basic shell), you can use the [[ ... ]]
conditional style, which has much cleaner syntax:
if [[ "${file: -4}" = "$1" || "${file: -4}" = "$2" ]]; then
Next, remove the do
before ffmpeg
. do
is part of the syntax for for
and while
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 the if
comparisons similarly, but that's more complicated).
Finally, you need a fi
(after the ffmpeg
line) to end the conditional. Every if
needs a then
and a fi
.
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 like do
and then
) on the same line. Anyway, here's my quick rewrite:
#!/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" ]]; then
#converting such files to the type of the first parameter using the FFMPEG comand
ffmpeg -i "$file" "${file%.*}$3"
fi
done
Solution 4
Even I got stuck with the same error. [https://i.stack.imgur.com/PsWGF.png] --> this link will take you to the error message.
The code which made me encounter this message is provided in the link below. [https://i.stack.imgur.com/Wom7q.png]
If you observe the error related to do command is followed by an error about the syntax of the while. You can get rid of the error just by introducing spaces around the square brackets. [https://i.stack.imgur.com/F1C7x.png]
This is due to the fact that the [ is not a shell-built-in rather it receives the expression as arguments. If [ is not surrounded by spaces the interpreter interprets it as $PATH.
I hope this answer helps you solve the problem.
Solution 5
Your script could be reduced to simply:
#!/bin/bash
for file in *; do
[[ ${file: -4} = $1 || ${file: -4} = $2 ]] && ffmpeg -i "$file" "${file%.*}$3"
done
Avi
Updated on August 23, 2020Comments
-
Avi almost 4 years
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
-
sjsam about 8 yearsTest your code with shellcheck
-
Avi about 8 yearsI did but I am a beginner in bash. I don't understand some of the error messages well on shellcheck. Any hint of where I went wrong would be greatly appreciated.
-
heemayl about 8 yearsTo be honest, i can see only the shebang line is correct in this script :|
-
-
Greg about 8 yearsGlad to hear it, although Gordon's answer below is much more thorough than mine - I corrected the 'if[' missing space, but forgot to mention it, and neglected to spot the missing 'then' also...
-
David Buck almost 4 yearsPlease don't post error messages and code as images (and if you do use images, the formatting guide will help you do so correctly). Code should always be added to questions and the answers as text. Images cannot be searched, and they are not accessible to people using assistive technology.