unexpected EOF while looking for matching `"' - bash script
You can see your problem if you just look at your question. Note how the syntax highlighting is screwed up after line 95:
echo -e "Sorry, an error occurred. You have to run this on OS X""
As the error message tells you, you have an unmatched "
. Just remove the extra "
from the line above and you should be fine:
echo -e "Sorry, an error occurred. You have to run this on OS X"
This error can be hard to track in real situations. Here I provide one solution for real-world situations. I will use my script as an example.
I updated my shell script. When executing it I got the following error message:
/somepath/bin/myshellscript: line 1508: unexpected EOF while looking for matching `"'
/somepath/bin/myshellscript: line 1520: syntax error: unexpected end of file
line 1508 elif [ "$project" ]; then
This is the last line that has a pair of double quotes.
Normally, I check my shell script every time I modify it. This time, I waited for one day and forgot where I had made a modification. The problem happened anywhere before this line (1508). The problem is that even I commented outline 1508
#elif [ "$project" ]; then
the shell executioner still said line 1508 has trouble.
Next, I made a copy of the original shell script. Deleting a whole bunch of code from the bottom. Then validate my code with the following command
bash -n mysbashscript
mybashscript: line 515: unexpected EOF while looking for matching `"'
mybashscript: line 561: syntax error: unexpected end of file
Now my file is 1/3 of the original size. I immediately saw the problem:
497 prepare_alignment() {
498 local projdir=${1:?"did not give project directory"}
499 local samp=${2:?"did not give sample name"}
500 local merged=${3:?"must give merged bam file name} # here is the problem
For some reason, the unmatched "
inside {}
is not captured by the shell parser. This is where shell parser can be further improved.
The fastest algorithm to find the problem is to delete half of your code from the bottom, if the syntax error goes away, then it is in this half. If the syntax error is still there, the problem in the upper half.
If the problem in the latter half, undo the delete. Repeat this process. You can narrow down to a smaller size to find the source of the problem.
When deleting code, you have to delete a whole section of code. For example the whole function.
You can either use bash -n script_name
or just directly execute the script. Both should work. (here script_name is just fooscript or barscript for illustration purpose not a real script name and should be one word).