Syntax Error: Unexpected End of File

Shell Script Syntax Error: Unexpected End of File

Edit: Note that the original post has been edited since this answer was written and has been reformatted. You should look at the history to see the original formatting to understand the context for this answer.

This error occurs often when you have mismatched structure - that is, you do not have matching double quotes, matching single quotes, have not closed a control structure such as a missing fi with an if, or a missing done with a for.

The best way to spot these is to use correct indentation, which will show you where you have a broken control structure, and syntax highlighting, which will show you where quotes are not matched.

In this particular case, I can see you are missing a fi. In the latter part of your code, you have 5 ifs and 4 fis. However you also have a number of other problems - your backquoted touch /tmp/alert.txt... command is syntactically invalid, and you need a space before the closing bracket of an if test.

Clean up your code, and errors start to stand out.

Bash syntax error: unexpected end of file

I think file.sh is with CRLF line terminators.

run

dos2unix file.sh

then the problem will be fixed.

You can install dos2unix in ubuntu with this:

sudo apt-get install dos2unix

getting syntax error: unexpected end of file in bash script

Use elif instead of else if.

Syntax of if in bash:

if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

syntax error, unexpected end of file running php7

   <?php
include("config.php");
$error=0;
if(isset($_POST['submit']))
{
// Set global variables to easier names
$title = $_POST['title'];
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
//check if (title) field is empty then print error message.
if(empty($title))
{ //this means If the title is really empty.
echo "Error: News title is a required field. Please fill it.";
$error=1;
}
}
?>
<html><body>
<br>
<h3>::Add News</h3>

<form method="post" action="<?php echo $PHP_SELF ?>">
Title: <input name="title" size="40" maxlength="255">
<br>
Text1: <textarea name="text1" rows="7" cols="30"></textarea>
<br>
Text2: <textarea name="text2" rows="7" cols="30"></textarea>
<br>
<input type="submit" name="submit" value="Add News">
</form>
</body></html>
<?php
if(isset($_POST['submit']) && $error==0)
{
//run the query which adds the data gathered from the form into the database
$sql = "INSERT INTO news (title, dtime, text1, text2)
VALUES ('$title',NOW(),'$text1','$text2')"
$result=$conn->query($sql);
if ($result==="TRUE")
echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}
?>

Hope this answers your question. If it does, please rate my answer.

PHP syntax error, unexpected end of file on my Yii2 run on php 7.2

Your heredoc declaration in _form.php is wrong. There shouldn't be a space between <<< and identifier at the start. The line for ending heredoc can contain only identifier and semicolon that means the identifier at the end of heredoc must not be indented.

Because you have the identifier indented the parser doesn't recognize it as end of heredoc declaration and parses the rest of file as string. Then when it comes to end it's complaining that it hadn't found the end of heredoc before the end of file.

This is how your heredoc declaration in _form.php should look like:

    <?php
$script = <<<JS
$('#mahasiswa-akses').on('change', function(){
var val = $(this).val();
document.getElementById("psd").style.display= val == 1 ? 'block' : 'none';
});

$(function(){
console.log(document.getElementById('mahasiswa-akses').value);
document.getElementById("psd").style.display= document.getElementById("mahasiswa-akses").value == 1 ? 'block' : 'none';
});
JS;

$this->registerJs($script,
yii\web\View::POS_END,
'in-x-handler'
);?>

You can find some examples of heredoc declaration in php documentation.

Also another thing that might cause problem if you are developing the script on windows machine than upload it to the linux/unix server is the character for end of line. The end of heredoc must be preceeded and followed by end of line character the current system is using. Windows uses \r\n as end of line but linux/unix systems uses only \n character.
So if you've developed the script on windows machine the end of your heredocs looks like:

JS;

But in linux/unix systems only \n is treated as new line and \r is left in string. That breaks the rule that only identifier and semicolon might be present in the end of heredoc.

\r
JS;\r

If you upload your scripts to server via FTP make sure you use text transfer for php files. The text transfer will convert the end of lines for you.
If you are using some versioning system like git there should be some settings that will allow you to convert the end of lines when commiting at your dev machine or pulling at your server.
Or you can set your editor to save files with unix style end of lines.

NOTE:
This requirements for heredoc are valid up to php 7.2.x. As noted by rob006 in comments the php 7.3 brought more flexible heredoc syntax that allows indentation and doesn't require ; and newline character right after ending identifier. But indentation still have its special meaning and can't be longer than identation of any line in the heredoc string. More details about heredoc/nowdoc changes.

syntax error: unexpected end of file (expecting fi)

<<- only strips tabs from the here-document; your closing delimiter appears to be indented (according to what Groovy actually presents to the shell) with a couple of spaces. Try something like

    sh """
#!/bin/bash
if [ ${TARGET_STAGE} == 'dev' ]; then
cat <<EOF | curl --data-binary @- \${BASTION_URL}/metrics/job/some_job
# TYPE some_metric counter
some_metric{label="val1"} 42
EOF
fi
"""

Note that as far as the shell executing the script is concerned, the here-document and the closing EOF aren't indented at all.

Bash keeps throwing syntax error: unexpected end of file

I used dos2unix on the file, and I was able to run it without errors.



Related Topics



Leave a reply



Submit