syntax error near unexpected token `do' in bash script
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
bash: syntax error near unexpected token `do
This is the giveaway:
'bash: working.sh: line 7: syntax error near unexpected token `do
'bash: working.sh: line 7: `do
See the '
at the front of the line? That's supposed to be printed at the end of the line.
What's sending the cursor back to the beginning of the line is a CR character, otherwise known as $'\r'
. Thus, instead of do
, you have do$'\r'
, and when the shell tries to print unexpected token `do'
, the CR sends the cursor to the beginning of the line, so the closing '
is printed there.
This happens because on DOS-style systems, newlines are two characters, CRLF, whereas on UNIX they're just CR.
-bash: syntax error near unexpected token `do'
Running the for
via sudo doesn't work as sudo expects a command. You can instead run the loop via bash:
sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt & done'
You wouldn't need to use seq
command as bash has the {0..9}
to support "range" loops.
See bash job control for more info on &
(which puts the "job" - the command you run - in the background).
bash script syntax error at function call and passing array as argument
The immediate error, just like the error message tells you, is that shell functions (just like shell scripts) do not require or accept commas between their arguments or parentheses around the argument list. But there are several changes you could make to improve this code.
Here's a refactored version, with inlined comments.
#!/bin/bash
urls=("example.com" "example2.com")
error_exit()
{
# Include script name in error message; echo all parameters
echo "$0: $@" 1>&2
exit 1
}
# A function can't really accept an array. But it's easy to fix:
# make the first argument the needle, and the rest, the haystack.
# Also, mark variables as local
index_of(){
local needle=$1
shift
local i
for ((i=1; i<=$#; ++i)); do
if [[ "${!i}" = "${needle}" ]]; then
echo "${i}"
# Return when you found it
return 0
fi
done
# Don't echo anything on failure; just return false
return 1
}
validate_url_param(){
# global ${urls[@]} is still a bit of a wart
if [ $# -eq 0 ]; then
error_exit "No url provided. Exiting"
else
if ! index_of "$1" "${urls[@]}"; then
error_exit "Provided url not found in list. Exiting"
fi
fi
}
# Just run the function from within the script itself
validate_url_param "example.com"
echo "${urls[0]}"
Notice how the validate_url_param
function doesn't capture the output from the function it is calling. index_of
simply prints the result to standard output and that's fine, just let that happen and don't intervene. The exit code tells us whether it succeeded or not.
However, reading the URLs into memory is often not useful or necessary. Perhaps you are simply looking for
grep -Fx example.com urls.txt
Why am I getting a line 10: syntax error near unexpected token `fi' error in bash script?
elif
expects another command to test. Formatting aside, local testing_file = $1
is treated as that condition, but then the keyword fi
is seen before the expected then
keyword.
Use else
instead:
if [ $1 = '' ]; then
local testing_file=myproject
else
local testing_file=$1
fi
(Note, too, that you cannot put spaces around the =
in the assignments.
Bash syntax issue, 'syntax error: unexpected do (expecting fi )'
The error message indicates that the script is executed as '/bin/sh', and not as /bin/bash. You can see the message with '/bin/sh -n script.sh'
Check how the script is invoked. On different systems /bin/sh is symlinked to bash or other shell that is less feature rich.
In particular, the problem is with the select
statement, included in bash, but not part of the POSIX standard.
Another option is that bash on your docker is set to be POSIX compliant by default
Related Topics
How to Change(Hide) the Nginx Server Signature
Installing Ghc Binaries on Linux (Can't Find Libgmp.So)
How to Redirect Cron Job Output to Stdout
What Is the Pid in the Host, of a Process Running Inside a Docker Container
How to Grep a String After a Specified Line Number
Linux: Modpost Does Not Build Anything
Best Posix Way to Determine If a Filesystem Is Mounted Read Only
Why Does the Wc Command Count One More Character Than Expected
Is There a Special Restriction on Commands Executed by Cron
Boost with Qt Creator and Linux
Combine File Modified Date and "Grep" Results Through "Find", in One Line
Stty Serial Port Settings for Parity Not Persistent
How to Parse Command Line Options in Bash Shell
Cannot Use Wildcard in Kernel Module Makefile
Extending a Script to Loop Over Multiple Files and Generate Output Names
Linux Shell to Restrict Sftp Users to Their Home Directories